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/manager.h>
#include <asterisk/ast_expr.h>
#include <asterisk/channel_pvt.h>
#include <asterisk/linkedlists.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <ctype.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 ;-)
*
*/
struct ast_context;
struct ast_pbx {
int dtimeout; /* Timeout between digits (seconds) */
int rtimeout; /* Timeout for response (seconds) */
};
/* 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 */
pthread_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);
static int pbx_builtin_prefix(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_setlanguage(struct ast_channel *, void *);
static int pbx_builtin_ringing(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_setvar(struct ast_channel *, void *);
static int pbx_builtin_gotoif(struct ast_channel *, void *);
void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
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
need separate modules */
"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" },
"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" },
"Unconditional hangup",
" Hangup(): Unconditionally hangs up a given channel by returning -1 always.\n" },
"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" },
"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" },
{ "AbsoluteTimeout", pbx_builtin_atimeout,
"Set absolute maximum time of call",
" AbsoluteTimeout(seconds): Set the absolute maximum amount of time permitted\n"
"for a call. Always returns 0.\n" },
"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" },
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"Waits for some time",
" Wait(seconds): Waits for a specified number of seconds, then returns 0.\n" },
{ "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" },
{ "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" },
{ "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 select a natural\n"
"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 'demo-\n"
"congrats-fr' exists, then it will play that file, and if not will play the\n"
"normal 'demo-congrats'. 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" },
{ "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." },
{ "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." },
{ "Setvar", pbx_builtin_setvar,
"Set variable to value",
" Setvar(#n=value): Sets variable n to value" },
{ "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." },
static pthread_mutex_t applock = AST_MUTEX_INITIALIZER;
static struct ast_context *contexts = NULL;
/* Lock for the ast_context list */
static pthread_mutex_t conlock = AST_MUTEX_INITIALIZER;
static pthread_mutex_t switchlock = AST_MUTEX_INITIALIZER;
struct ast_switch *switches = 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;
int stack = c->stack;
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);
/* 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
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;
}
static struct ast_switch *pbx_findswitch(char *sw)
{
struct ast_switch *asw;
if (ast_pthread_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_pthread_mutex_unlock(&switchlock);
return asw;
}
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
static inline int include_valid(struct ast_include *i)
{
struct tm *tm;
time_t t;
if (!i->hastime)
return 1;
time(&t);
tm = localtime(&t);
if (!tm) {
ast_log(LOG_WARNING, "Failed to get local time\n");
return 0;
}
/* If it's not the right month, return */
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)))
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);
}
int ast_extension_match(char *pattern, char *data)
{
int match;
/* If they're the same return */
if (!strcasecmp(pattern, data))
return 1;
/* All patterns begin with _ */
if (pattern[0] != '_')
return 0;
/* Start optimistic */
match=1;
pattern++;
while(match && *data && *pattern && (*pattern != '/')) {
switch(toupper(*pattern)) {
case 'N':
if ((*data < '2') || (*data > '9'))
match=0;
break;
case 'X':
if ((*data < '0') || (*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++;
}
/* Must be at the end of both */
if (*data || (*pattern && (*pattern != '/')))
match = 0;
static int extension_close(char *pattern, char *data)
{
int match;
/* If "data" is longer, it can'be a subset of pattern */
if (strlen(pattern) < strlen(data))
return 0;
if (!strlen((char *)data) || !strncasecmp(pattern, data, strlen(data))) {
return 1;
}
/* All patterns begin with _ */
if (pattern[0] != '_')
return 0;
/* Start optimistic */
match=1;
pattern++;
while(match && *data && *pattern && (*pattern != '/')) {
switch(toupper(*pattern)) {
case 'N':
if ((*data < '2') || (*data > '9'))
match=0;
break;
case 'X':
if ((*data < '0') || (*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++;
}
return match;
}
struct ast_context *ast_context_find(char *name)
{
struct ast_context *tmp;
ast_pthread_mutex_lock(&conlock);
if (name) {
tmp = contexts;
while(tmp) {
if (!strcasecmp(name, tmp->name))
break;
tmp = tmp->next;
}
} else
tmp = contexts;
ast_pthread_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 (strlen(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 (!strcasecmp(tmp->name, context)) {
if (*status < STATUS_NO_EXTENSION)
*status = STATUS_NO_EXTENSION;
eroot = tmp->root;
while(eroot) {
/* Match extension */
if ((ast_extension_match(eroot->exten, exten) ||
((action == HELPER_CANMATCH) && (extension_close(eroot->exten, exten)))) &&
(!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
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;
}
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
static void *pbx_substitute_variables(struct ast_channel *c, struct ast_exten *e) {
char *cp1,*cp3,*cp4,*cp5;
void *cp2;
char c1,c2;
int m,mve,origlen,quoted,dolsign,docopy;
struct ast_var_t *variables;
struct varshead *headp;
char pri[80];
headp=&c->varshead;
origlen=strlen(e->data)+1;
cp2=malloc(origlen);
memset(cp2,0,origlen);
if ((strchr(e->data,'$')==NULL) && (strchr(e->data,'[')==NULL)) {
strncpy(cp2,e->data,strlen(e->data));
return(cp2);
/* No variables or expressions in e->data, so why scan it? */
}
cp4=NULL;
cp1=e->data;
quoted=0;
dolsign=0;
docopy=1;
/* First stage, variable substitution */
do {
c1=*cp1;
mve=0;
switch (c1) {
case '\\' :
dolsign=0;
if (quoted==1) {
quoted=0;
docopy=1;
} else {
quoted=1;
docopy=0;
}
break;
case '$' :
if (quoted==1) {
quoted=0;
docopy=1;
dolsign=0;
} else {
docopy=0;
dolsign=1;
}
break;
case '{' :
if (quoted==1) {
quoted=0;
dolsign=0;
docopy=1;
break;
}
if (dolsign==0) {
docopy=1;
break;
}
docopy=0;
dolsign=0;
m=0;
cp1++;
while (((c2=*(cp1+m))!='}') && (c2!='\0')) {
m++;
}
mve=1;
cp3=malloc(m+2);
strncpy(cp3,cp1,m);
cp3[m]='\0';
cp1+=m;
/* Now we have the variable name on cp3 */
if (!strcmp(cp3, "CALLERID")) {
cp4 = c->callerid;
break;
} else if (!strcmp(cp3, "EXTEN")) {
cp4 = c->exten;
break;
} else if (!strcmp(cp3, "CONTEXT")) {
cp4 = c->context;
break;
} else if (!strcmp(cp3, "PRIORITY")) {
snprintf(pri, sizeof(pri), "%d", c->priority);
cp4 = pri;
break;
} else {
AST_LIST_TRAVERSE(headp,variables,entries) {
// ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",cp3,ast_var_name(variables));
if (strncasecmp(ast_var_name(variables),cp3,m)==0) {
cp4=ast_var_value(variables);
break;
}
}
}
free(cp3);
break;
default :
if (dolsign==1) {
strncat((char *)cp2,"$",1);
}
if (quoted==1) {
quoted=0;
}
mve=0;
dolsign=0;
docopy=1;
break;
}
if (cp1!='\0') {
if (mve==0) {
if (docopy==1) {
strncat((char *)cp2,&c1,1);
}
} else {
if (cp4!=NULL) {
cp2=realloc(cp2,origlen+strlen(cp4)+1);
strncat((char *)cp2,cp4,strlen(cp4));
} else {
ast_log(LOG_WARNING,"mve!=0 and cp4=NULL, something gone astray\n");
}
}
}
} while (*cp1++!='\0');
/* Second stage, expression evaluation */
if ((strstr(cp2,"$[")==NULL)) {
/* No expressions in cp2, return it */
return(cp2);
}
/* else, do expression evaluation */
dolsign=0;
docopy=1;
origlen=strlen(cp2)+1;
cp5=malloc(origlen);
memset(cp5,0,origlen);
cp4=NULL;
cp1=cp2;
quoted=0;
do {
c1=*cp1;
mve=0;
switch (c1) {
case '$' :
dolsign=1;
docopy=0;
break;
case '[' :
if (dolsign==0) {
docopy=1;
dolsign=0;
break;
}
dolsign=0;
docopy=0;
m=0;
mve=1;
cp1++;
while (((c2=*(cp1+m))!=']') && (c2!='\0')) {
m++;
}
cp3=malloc(m+2);
strncpy(cp3,cp1,m);
cp3[m]='\0';
cp1+=m;
/* Now we have the expression to evaluate on cp3 */
cp4=ast_expr(cp3);
free(cp3);
break;
default :
if (dolsign==1) {
strncat((char *)cp5,"$",1);
}
dolsign=0;
docopy=1;
mve=0;
break;
}
if (cp1!='\0') {
if (mve==0) {
if (docopy==1) {
strncat((char *)cp5,&c1,1);
}
} else {
if (cp4!=NULL) {
cp5=realloc(cp5,origlen+strlen(cp4)+1);
strncat((char *)cp5,cp4,strlen(cp4));
free(cp4);
} else {
ast_log(LOG_WARNING,"mve!=0 and cp4=NULL, something gone astray\n");
}
}
}
} while (*cp1++!='\0');
free(cp2);
return(cp5);
}
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];
int stacklen = 0;
char tmp[80];
char tmp2[80];
char tmp3[256];
if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH))
e = pbx_find_extension(c, context, exten, priority, callerid, action, incstack, &stacklen, &status, &sw, &data);
if (e) {
switch(action) {
case HELPER_CANMATCH:
pthread_mutex_unlock(&conlock);
return -1;
case HELPER_EXISTS:
pthread_mutex_unlock(&conlock);
return -1;
case HELPER_SPAWN:
newstack++;
/* Fall through */
case HELPER_EXEC:
app = pbx_findapp(e->app);
pthread_mutex_unlock(&conlock);
if (app) {
strncpy(c->context, context, sizeof(c->context-1));
strncpy(c->exten, exten, sizeof(c->exten)-1);
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, (newdata ? (char *)newdata : NULL), COLOR_BRMAGENTA, 0, sizeof(tmp3)),
res = pbx_exec(c, app, newdata, newstack);
free(newdata);
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:
pthread_mutex_unlock(&conlock);
return -1;
case HELPER_EXISTS:
pthread_mutex_unlock(&conlock);
return -1;
case HELPER_SPAWN:
newstack++;
/* Fall through */
case HELPER_EXEC:
pthread_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;
}
} else {
pthread_mutex_unlock(&conlock);
switch(status) {
case STATUS_NO_CONTEXT:
if (action != HELPER_EXISTS)
ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
break;
case STATUS_NO_EXTENSION:
if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH))
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))
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))
return -1;
else
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_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_WARNING, "Out of memory\n");
return -1;
}
ast_log(LOG_WARNING, "%s already has a call record??\n", c->name);