dect
/
asterisk
Archived
13
0
Fork 0

major re-work of dialplan functions, including:

- locking of functions list during registration/unregistration/searching
 - rename of function description structure to be consistent with the rest of the API
 - addition of 'desc' element to description structure, for detailed description (like applications)
 - addition of 'show function' CLI command to show function details
 - conversion of existing functions to use uppercase names to match policy
 - creation of new 'pbx_functions.so' module to contain standard 'builtin' functions
 - removal of all builtin functions from pbx.c and apps and placement into new 'funcs' directory


git-svn-id: http://svn.digium.com/svn/asterisk/trunk@5583 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
kpfleming 2005-05-05 05:39:33 +00:00
parent 7fe9220d99
commit 6a262d98ee
14 changed files with 835 additions and 459 deletions

View File

@ -233,7 +233,7 @@ CFLAGS+= $(MALLOC_DEBUG)
CFLAGS+= $(BUSYDETECT)
CFLAGS+= $(OPTIONS)
CFLAGS+= -fomit-frame-pointer
SUBDIRS=res channels pbx apps codecs formats agi cdr utils stdtime
SUBDIRS=res channels pbx apps codecs formats agi cdr funcs utils stdtime
ifeq (${OSARCH},Linux)
LIBS=-ldl -lpthread
endif

View File

@ -169,44 +169,14 @@ static int cut_exec(struct ast_channel *chan, void *data)
return res;
}
static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *varname, *varval="", workspace[256];
char *delim = ast_strdupa(data);
int fieldcount=0;
if (delim) {
varname = strsep(&delim, "|");
pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL);
while (strsep(&varval, delim)) {
fieldcount++;
}
snprintf(buf, len, "%d", fieldcount);
} else {
ast_log(LOG_ERROR, "Out of memory\n");
strncpy(buf, "1", len);
}
return buf;
}
static struct ast_custom_function_obj fieldqty_function = {
.name = "FIELDQTY",
.desc = "Count the fields, with an arbitrary delimiter",
.syntax = "FIELDQTY(<varname>,<delim>)",
.read = function_fieldqty,
.write = NULL,
};
int unload_module(void)
{
STANDARD_HANGUP_LOCALUSERS;
ast_custom_function_unregister(&fieldqty_function);
return ast_unregister_application(app_cut);
}
int load_module(void)
{
ast_custom_function_register(&fieldqty_function);
return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
}

View File

@ -34,68 +34,6 @@ LOCAL_USER_DECL;
static int deprecation_warning = 0;
static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int count;
struct localuser *u;
char group[80] = "";
char category[80] = "";
char *grp;
LOCAL_USER_ADD(u);
ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
if (ast_strlen_zero(group)) {
grp = pbx_builtin_getvar_helper(chan, category);
strncpy(group, grp, sizeof(group) - 1);
}
count = ast_app_group_get_count(group, category);
snprintf(buf, len, "%d", count);
LOCAL_USER_REMOVE(u);
return buf;
}
static struct ast_custom_function_obj group_count_function_obj = {
.name = "GROUP_COUNT",
.desc = "Calculates the group count for the specified group, or uses the current channel's group if not specifed (and non-empty).",
.syntax = "GROUP_COUNT([groupname][@category])",
.read = group_count_function_read,
.write = NULL,
};
static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int count;
struct localuser *u;
char group[80] = "";
char category[80] = "";
LOCAL_USER_ADD(u);
ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
if (!ast_strlen_zero(group)) {
count = ast_app_group_match_get_count(group, category);
snprintf(buf, len, "%d", count);
}
LOCAL_USER_REMOVE(u);
return buf;
}
static struct ast_custom_function_obj group_match_count_function_obj = {
.name = "GROUP_MATCH_COUNT",
.desc = "Calculates the group count for all groups that match the specified pattern. Uses standard regular expression matching (see regex(7)).",
.syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
.read = group_match_count_function_read,
.write = NULL,
};
static int group_count_exec(struct ast_channel *chan, void *data)
{
int res = 0;
@ -311,8 +249,6 @@ int unload_module(void)
res |= ast_unregister_application(app_group_set);
res |= ast_unregister_application(app_group_check);
res |= ast_unregister_application(app_group_match_count);
res |= ast_custom_function_unregister(&group_count_function_obj);
res |= ast_custom_function_unregister(&group_match_count_function_obj);
return res;
}
@ -323,8 +259,6 @@ int load_module(void)
res |= ast_register_application(app_group_set, group_set_exec, group_set_synopsis, group_set_descrip);
res |= ast_register_application(app_group_check, group_check_exec, group_check_synopsis, group_check_descrip);
res |= ast_register_application(app_group_match_count, group_match_count_exec, group_match_count_synopsis, group_match_count_descrip);
res |= ast_custom_function_register(&group_count_function_obj);
res |= ast_custom_function_register(&group_match_count_function_obj);
ast_cli_register(&cli_show_channels);
return res;
}

View File

@ -7875,9 +7875,9 @@ static struct ast_cli_entry cli_no_history =
static struct ast_cli_entry cli_no_debug =
{ { "sip", "no", "debug", NULL }, sip_no_debug, "Disable SIP debugging", no_debug_usage };
static struct ast_custom_function_obj sip_header_function = {
static struct ast_custom_function sip_header_function = {
.name = "SIP_HEADER",
.desc = "Gets or sets the specified SIP header",
.synopsis = "Gets or sets the specified SIP header",
.syntax = "SIP_HEADER(<name>)",
.read = func_header_read,
};

58
funcs/Makefile Executable file
View File

@ -0,0 +1,58 @@
#
# Asterisk -- A telephony toolkit for Linux.
#
# Makefile for dialplan functions
#
# Copyright (C) 2005, Digium, Inc.
#
# Kevin P. Fleming <kpfleming@digium.com>
#
# This program is free software, distributed under the terms of
# the GNU General Public License
#
FUNCS=pbx_functions.so
BUILTINS=func_md5.o func_groupcount.o func_strings.o func_cdr.o func_logic.o func_env.o
STANDALONE_FUNCS=$(filter-out $(BUILTINS),$(patsubst %.c,%.o,$(wildcard func*.c)))
FUNCS+=$(STANDALONE_FUNCS:.o=.so)
FUNC_STRUCTS=$(shell grep 'struct ast_custom_function' $(BUILTINS:.o=.c) | awk '{print $$3};')
all: $(FUNCS)
clean:
rm -f *.so *.o .depend
%.so : %.o
$(CC) $(SOLINK) -o $@ $<
$(BUILTINS) : CFLAGS += -DBUILTIN_FUNC
pbx_functions.h: $(BUILTINS:.o=.c)
@echo "/* Automatically generated - do not edit */" > $@
@for f in $(FUNC_STRUCTS); do echo "extern struct ast_custom_function $$f;" >> $@; done
@echo "static struct ast_custom_function *builtins[] = {" >> $@
@for f in $(FUNC_STRUCTS); do echo "&$$f," >> $@; done
@echo "};" >> $@
pbx_functions.so: pbx_functions.o $(BUILTINS)
$(CC) $(SOLINK) -o $@ $(BUILTINS) $<
strip $(foreach f,$(FUNC_STRUCTS),-N $(f)) $@
install: all
for x in $(FUNCS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
ifneq ($(wildcard .depend),)
include .depend
endif
depend: .depend
.depend: pbx_functions.h
../mkdep $(CFLAGS) `ls *.c`
env:
env

89
funcs/func_cdr.c Executable file
View File

@ -0,0 +1,89 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* MD5 digest related dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/cdr.h"
static char *builtin_function_cdr_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret;
char *mydata;
int argc;
char *argv[2];
int recursive = 0;
if (!data || ast_strlen_zero(data))
return NULL;
if (!chan->cdr)
return NULL;
mydata = ast_strdupa(data);
argc = ast_separate_app_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
/* check for a trailing flags argument */
if (argc > 1) {
argc--;
if (strchr(argv[argc], 'r'))
recursive = 1;
}
ast_cdr_getvar(chan->cdr, argv[0], &ret, buf, len, recursive);
return ret;
}
static void builtin_function_cdr_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
{
char *mydata;
int argc;
char *argv[2];
int recursive = 0;
if (!data || ast_strlen_zero(data) || !value)
return;
if (!chan->cdr)
return;
mydata = ast_strdupa(data);
argc = ast_separate_app_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
/* check for a trailing flags argument */
if (argc > 1) {
argc--;
if (strchr(argv[argc], 'r'))
recursive = 1;
}
ast_cdr_setvar(chan->cdr, argv[0], value, recursive);
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function cdr_function = {
.name = "CDR",
.synopsis = "Gets or sets a CDR variable",
.desc= "Option 'r' searches the entire stack of CDRs on the channel\n",
.syntax = "CDR(<name>[|options])",
.read = builtin_function_cdr_read,
.write = builtin_function_cdr_write,
};

56
funcs/func_env.c Executable file
View File

@ -0,0 +1,56 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* MD5 digest related dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
static char *builtin_function_env_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = "";
if (data) {
ret = getenv(data);
if (!ret)
ret = "";
}
ast_copy_string(buf, ret, len);
return buf;
}
static void builtin_function_env_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
{
if (data && !ast_strlen_zero(data)) {
if (value && !ast_strlen_zero(value)) {
setenv(data, value, 1);
} else {
unsetenv(data);
}
}
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function env_function = {
.name = "ENV",
.synopsis = "Gets or sets the environment variable specified",
.syntax = "ENV(<envname>)",
.read = builtin_function_env_read,
.write = builtin_function_env_write,
};

81
funcs/func_groupcount.c Executable file
View File

@ -0,0 +1,81 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Channel group related dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int count;
char group[80] = "";
char category[80] = "";
char *grp;
ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
if (ast_strlen_zero(group)) {
grp = pbx_builtin_getvar_helper(chan, category);
strncpy(group, grp, sizeof(group) - 1);
}
count = ast_app_group_get_count(group, category);
snprintf(buf, len, "%d", count);
return buf;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function group_count_function = {
.name = "GROUP_COUNT",
.syntax = "GROUP_COUNT([groupname][@category])",
.synopsis = "Counts the number of channels in the specified group",
.desc = "Calculates the group count for the specified group, or uses the\n"
"channel's current group if not specifed (and non-empty).\n",
.read = group_count_function_read,
};
static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int count;
char group[80] = "";
char category[80] = "";
ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
if (!ast_strlen_zero(group)) {
count = ast_app_group_match_get_count(group, category);
snprintf(buf, len, "%d", count);
}
return buf;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function group_match_count_function = {
.name = "GROUP_MATCH_COUNT",
.syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
.synopsis = "Counts the number of channels in the groups matching the specified pattern",
.desc = "Calculates the group count for all groups that match the specified pattern.\n"
"Uses standard regular expression matching (see regex(7)).\n",
.read = group_match_count_function_read,
.write = NULL,
};

100
funcs/func_logic.c Executable file
View File

@ -0,0 +1,100 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* MD5 digest related dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/config.h" /* for ast_true */
static char *builtin_function_isnull(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0";
return data && *data ? ret_false : ret_true;
}
static char *builtin_function_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0";
return data && *data ? ret_true : ret_false;
}
static char *builtin_function_if(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;
char *mydata = NULL;
char *expr = NULL;
char *iftrue = NULL;
char *iffalse = NULL;
if((mydata = ast_strdupa(data))) {
expr = mydata;
if ((iftrue = strchr(mydata, '?'))) {
*iftrue = '\0';
iftrue++;
if ((iffalse = strchr(iftrue, ':'))) {
*iffalse = '\0';
iffalse++;
}
} else
iffalse = "";
if (expr && iftrue) {
ret = ast_true(expr) ? iftrue : iffalse;
strncpy(buf, ret, len);
ret = buf;
} else {
ast_log(LOG_WARNING, "Syntax $(if <expr>?[<truecond>][:<falsecond>])\n");
ret = NULL;
}
} else {
ast_log(LOG_WARNING, "Memory Error!\n");
ret = NULL;
}
return ret;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function isnull_function = {
.name = "ISNULL",
.synopsis = "NULL Test: Returns 1 if NULL or 0 otherwise",
.syntax = "ISNULL(<data>)",
.read = builtin_function_isnull,
};
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function exists_function = {
.name = "EXISTS",
.synopsis = "Existence Test: Returns 1 if exists, 0 otherwise",
.syntax = "EXISTS(<data>)",
.read = builtin_function_exists,
};
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function if_function = {
.name = "IF",
.synopsis = "Conditional: Returns the data following '?' if true else the data following ':'",
.syntax = "IF(<expr>?<true>:<false>)",
.read = builtin_function_if,
};

86
funcs/func_md5.c Executable file
View File

@ -0,0 +1,86 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* MD5 digest related dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
static char *builtin_function_md5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char md5[33];
if (!data || ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "Syntax: MD5(<data>) - missing argument!\n");
return NULL;
}
ast_md5_hash(md5, data);
ast_copy_string(buf, md5, len);
return buf;
}
static char *builtin_function_checkmd5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int argc;
char *argv[2];
char *args;
char newmd5[33];
if (!data || ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
return NULL;
}
args = ast_strdupa(data);
argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0]));
if (argc < 2) {
ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
return NULL;
}
ast_md5_hash(newmd5, argv[1]);
if (!strcasecmp(newmd5, argv[0])) /* they match */
ast_copy_string(buf, "1", len);
else
ast_copy_string(buf, "0", len);
return buf;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function md5_function = {
.name = "MD5",
.synopsis = "Computes an MD5 digest",
.syntax = "MD5(<data>)",
.read = builtin_function_md5,
};
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function checkmd5_function = {
.name = "CHECK_MD5",
.synopsis = "Checks an MD5 digest",
.desc = "Returns 1 on a match, 0 otherwise\n",
.syntax = "CHECK_MD5(<digest>,<data>)",
.read = builtin_function_checkmd5,
};

117
funcs/func_strings.c Executable file
View File

@ -0,0 +1,117 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* String manipulation dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *varname, *varval, workspace[256];
char *delim = ast_strdupa(data);
int fieldcount = 0;
if (delim) {
varname = strsep(&delim, "|");
pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL);
while (strsep(&varval, delim))
fieldcount++;
snprintf(buf, len, "%d", fieldcount);
} else {
ast_log(LOG_ERROR, "Out of memory\n");
strncpy(buf, "1", len);
}
return buf;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function fieldqty_function = {
.name = "FIELDQTY",
.synopsis = "Count the fields, with an arbitrary delimiter",
.syntax = "FIELDQTY(<varname>,<delim>)",
.read = function_fieldqty,
};
static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0", *ret;
char *arg, *earg, *tmp, errstr[256] = "";
int errcode;
regex_t regexbuf;
ret = ret_false; /* convince me otherwise */
tmp = ast_strdupa(data);
if (tmp) {
/* Regex in quotes */
arg = strchr(tmp, '"');
if (arg) {
arg++;
earg = strrchr(arg, '"');
if (earg) {
*earg = '\0';
}
} else {
arg = tmp;
}
if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
regerror(errcode, &regexbuf, errstr, sizeof(errstr));
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
ret = NULL;
} else {
ret = regexec(&regexbuf, data, 0, NULL, 0) ? ret_false : ret_true;
}
regfree(&regexbuf);
} else {
ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
}
return ret;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function regex_function = {
.name = "REGEX",
.synopsis = "Regular Expression: Returns 1 if data matches regular expression.",
.syntax = "REGEX(\"<regular expression>\" <data>)",
.read = builtin_function_regex,
};
static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int length = 0;
if (data) {
length = strlen(data);
}
snprintf(buf, len, "%d", length);
return buf;
}
#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function len_function = {
.name = "LEN",
.synopsis = "Returns the length of the argument given",
.syntax = "LEN(<string>)",
.read = builtin_function_len,
};

58
funcs/pbx_functions.c Executable file
View File

@ -0,0 +1,58 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Builtin dialplan functions
*
* Copyright (C) 2005, Digium, Inc.
*
* Kevin P. Fleming <kpfleming@digium.com>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <sys/types.h>
#include <stdlib.h>
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "pbx_functions.h"
static char *tdesc = "Builtin dialplan functions";
int unload_module(void)
{
int x;
for (x = 0; x < (sizeof(builtins) / sizeof(builtins[0])); x++) {
ast_custom_function_unregister(builtins[x]);
}
return 0;
}
int load_module(void)
{
int x;
for (x = 0; x < (sizeof(builtins) / sizeof(builtins[0])); x++) {
ast_custom_function_register(builtins[x]);
}
return 0;
}
char *description(void)
{
return tdesc;
}
int usecount(void)
{
return 0;
}
char *key()
{
return ASTERISK_GPL_KEY;
}

View File

@ -53,14 +53,15 @@ typedef int (*ast_state_cb_type)(char *context, char* id, int state, void *data)
typedef int (*ast_devstate_cb_type)(const char *dev, int state, void *data);
/*! Data structure associated with an asterisk custom function */
struct ast_custom_function_obj {
/*! Data structure associated with a custom function */
struct ast_custom_function {
char *name;
char *synopsis;
char *desc;
char *syntax;
char *(*read)(struct ast_channel *, char *, char *, char *, size_t);
void (*write)(struct ast_channel *, char *, char *, const char *);
struct ast_custom_function_obj *next;
struct ast_custom_function *next;
};
/*! Data structure associated with an asterisk switch */
@ -597,9 +598,10 @@ int ast_goto_if_exists(struct ast_channel *chan, char* context, char *exten, int
int ast_parseable_goto(struct ast_channel *chan, const char *goto_string);
int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority);
int ast_async_goto_if_exists(struct ast_channel *chan, char* context, char *exten, int priority);
struct ast_custom_function_obj* ast_custom_function_find_obj(char *name);
int ast_custom_function_unregister(struct ast_custom_function_obj *acf);
int ast_custom_function_register(struct ast_custom_function_obj *acf);
struct ast_custom_function* ast_custom_function_find(char *name);
int ast_custom_function_unregister(struct ast_custom_function *acf);
int ast_custom_function_register(struct ast_custom_function *acf);
#if defined(__cplusplus) || defined(c_plusplus)
}

533
pbx.c
View File

@ -12,7 +12,6 @@
*/
#include <sys/types.h>
#include <regex.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
@ -215,7 +214,8 @@ static struct varshead globals;
static int autofallthrough = 0;
static struct ast_custom_function_obj *acf_root = NULL;
AST_MUTEX_DEFINE_STATIC(acflock); /* Lock for the custom function list */
static struct ast_custom_function *acf_root = NULL;
static struct pbx_builtin {
char name[AST_MAX_APP];
@ -1101,75 +1101,187 @@ icky:
static int handle_show_functions(int fd, int argc, char *argv[])
{
struct ast_custom_function_obj *acfptr;
struct ast_custom_function *acf;
ast_cli(fd, "Installed Custom Functions:\n--------------------------------------------------------------------------------\n");
for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
ast_cli(fd, "%s\t(%s)\t[%s]\n", acfptr->name, acfptr->desc, acfptr->syntax);
for (acf = acf_root ; acf; acf = acf->next) {
ast_cli(fd, "%s\t(%s)\t[%s]\n", acf->name, acf->synopsis, acf->syntax);
}
ast_cli(fd, "\n");
return 0;
}
struct ast_custom_function_obj* ast_custom_function_find_obj(char *name)
static int handle_show_function(int fd, int argc, char *argv[])
{
struct ast_custom_function_obj *acfptr;
struct ast_custom_function *acf;
/* Maximum number of characters added by terminal coloring is 22 */
char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
char stxtitle[40], *syntax = NULL;
int synopsis_size, description_size, syntax_size;
for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
if (argc < 3) return RESULT_SHOWUSAGE;
if (!(acf = ast_custom_function_find(argv[2]))) {
ast_cli(fd, "No function by that name registered.\n");
return RESULT_FAILURE;
}
if (acf->synopsis)
synopsis_size = strlen(acf->synopsis) + 23;
else
synopsis_size = strlen("Not available") + 23;
synopsis = alloca(synopsis_size);
if (acf->desc)
description_size = strlen(acf->desc) + 23;
else
description_size = strlen("Not available") + 23;
description = alloca(description_size);
if (acf->syntax)
syntax_size = strlen(acf->syntax) + 23;
else
syntax_size = strlen("Not available") + 23;
syntax = alloca(syntax_size);
snprintf(info, 64 + AST_MAX_APP, "\n -= Info about function '%s' =- \n\n", acf->name);
term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
term_color(stxtitle, "[Syntax]\n", COLOR_MAGENTA, 0, 40);
term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
term_color(syntax,
acf->syntax ? acf->syntax : "Not available",
COLOR_CYAN, 0, syntax_size);
term_color(synopsis,
acf->synopsis ? acf->synopsis : "Not available",
COLOR_CYAN, 0, synopsis_size);
term_color(description,
acf->desc ? acf->desc : "Not available",
COLOR_CYAN, 0, description_size);
ast_cli(fd,"%s%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, syntitle, synopsis, destitle, description);
return RESULT_SUCCESS;
}
static char *complete_show_function(char *line, char *word, int pos, int state)
{
struct ast_custom_function *acf;
int which = 0;
/* try to lock functions list ... */
if (ast_mutex_lock(&acflock)) {
ast_log(LOG_ERROR, "Unable to lock function list\n");
return NULL;
}
acf = acf_root;
while (acf) {
if (!strncasecmp(word, acf->name, strlen(word))) {
if (++which > state) {
char *ret = strdup(acf->name);
ast_mutex_unlock(&acflock);
return ret;
}
}
acf = acf->next;
}
ast_mutex_unlock(&acflock);
return NULL;
}
struct ast_custom_function* ast_custom_function_find(char *name)
{
struct ast_custom_function *acfptr;
/* try to lock functions list ... */
if (ast_mutex_lock(&acflock)) {
ast_log(LOG_ERROR, "Unable to lock function list\n");
return NULL;
}
for (acfptr = acf_root; acfptr; acfptr = acfptr->next) {
if (!strcmp(name, acfptr->name)) {
break;
}
}
}
ast_mutex_unlock(&acflock);
return acfptr;
}
int ast_custom_function_unregister(struct ast_custom_function_obj *acf)
int ast_custom_function_unregister(struct ast_custom_function *acf)
{
struct ast_custom_function_obj *acfptr, *lastacf = NULL;
struct ast_custom_function *acfptr, *lastacf = NULL;
int res = -1;
if (acf) {
for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
if (acfptr == acf) {
if (lastacf) {
lastacf->next = acf->next;
} else {
acf_root = acf->next;
}
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Unregistered custom function %s\n", acf->name);
return 0;
}
lastacf = acfptr;
}
if (!acf)
return -1;
/* try to lock functions list ... */
if (ast_mutex_lock(&acflock)) {
ast_log(LOG_ERROR, "Unable to lock function list\n");
return -1;
}
return -1;
for (acfptr = acf_root; acfptr; acfptr = acfptr->next) {
if (acfptr == acf) {
if (lastacf) {
lastacf->next = acf->next;
} else {
acf_root = acf->next;
}
res = 0;
break;
}
lastacf = acfptr;
}
ast_mutex_unlock(&acflock);
if (!res && (option_verbose > 1))
ast_verbose(VERBOSE_PREFIX_2 "Unregistered custom function %s\n", acf->name);
return res;
}
int ast_custom_function_register(struct ast_custom_function_obj *acf)
int ast_custom_function_register(struct ast_custom_function *acf)
{
struct ast_custom_function_obj *acfptr;
if (!acf)
return -1;
if (acf) {
if((acfptr = ast_custom_function_find_obj(acf->name))) {
ast_log(LOG_ERROR, "Function %s already in use.\n", acf->name);
return -1;
}
acf->next = acf_root;
acf_root = acf;
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Registered custom function %s\n", acf->name);
return 0;
/* try to lock functions list ... */
if (ast_mutex_lock(&acflock)) {
ast_log(LOG_ERROR, "Unable to lock function list\n");
return -1;
}
return -1;
if (ast_custom_function_find(acf->name)) {
ast_log(LOG_ERROR, "Function %s already registered.\n", acf->name);
ast_mutex_unlock(&acflock);
return -1;
}
acf->next = acf_root;
acf_root = acf;
ast_mutex_unlock(&acflock);
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Registered custom function %s\n", acf->name);
return 0;
}
char *ast_func_read(struct ast_channel *chan, const char *in, char *workspace, size_t len)
{
char *args = NULL, *function, *p;
char *ret = "0";
struct ast_custom_function_obj *acfptr;
struct ast_custom_function *acfptr;
function = ast_strdupa(in);
if (function) {
@ -1185,7 +1297,7 @@ char *ast_func_read(struct ast_channel *chan, const char *in, char *workspace, s
ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n");
}
if ((acfptr = ast_custom_function_find_obj(function))) {
if ((acfptr = ast_custom_function_find(function))) {
/* run the custom function */
if (acfptr->read) {
return acfptr->read(chan, function, args, workspace, len);
@ -1204,7 +1316,7 @@ char *ast_func_read(struct ast_channel *chan, const char *in, char *workspace, s
static void ast_func_write(struct ast_channel *chan, const char *in, const char *value)
{
char *args = NULL, *function, *p;
struct ast_custom_function_obj *acfptr;
struct ast_custom_function *acfptr;
function = ast_strdupa(in);
if (function) {
@ -1220,7 +1332,7 @@ static void ast_func_write(struct ast_channel *chan, const char *in, const char
ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n");
}
if ((acfptr = ast_custom_function_find_obj(function))) {
if ((acfptr = ast_custom_function_find(function))) {
/* run the custom function */
if (acfptr->write) {
acfptr->write(chan, function, args, value);
@ -1235,224 +1347,6 @@ static void ast_func_write(struct ast_channel *chan, const char *in, const char
}
}
static char *builtin_function_isnull(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0";
return data && *data ? ret_false : ret_true;
}
static char *builtin_function_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0";
return data && *data ? ret_true : ret_false;
}
static char *builtin_function_if(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;
char *mydata = NULL;
char *expr = NULL;
char *iftrue = NULL;
char *iffalse = NULL;
if((mydata = ast_strdupa(data))) {
expr = mydata;
if ((iftrue = strchr(mydata, '?'))) {
*iftrue = '\0';
iftrue++;
if ((iffalse = strchr(iftrue, ':'))) {
*iffalse = '\0';
iffalse++;
}
} else
iffalse = "";
if (expr && iftrue) {
ret = ast_true(expr) ? iftrue : iffalse;
strncpy(buf, ret, len);
ret = buf;
} else {
ast_log(LOG_WARNING, "Syntax $(if <expr>?[<truecond>][:<falsecond>])\n");
ret = NULL;
}
} else {
ast_log(LOG_WARNING, "Memory Error!\n");
ret = NULL;
}
return ret;
}
static char *builtin_function_env_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = "";
if (data) {
ret = getenv(data);
if (!ret)
ret = "";
}
strncpy(buf, ret, len);
buf[len - 1] = '\0';
return buf;
}
static void builtin_function_env_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
{
if (data && !ast_strlen_zero(data)) {
if (value && !ast_strlen_zero(value)) {
setenv(data, value, 1);
} else {
unsetenv(data);
}
}
}
static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int length = 0;
if (data) {
length = strlen(data);
}
snprintf(buf, len, "%d", length);
return buf;
}
static char *builtin_function_cdr_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret;
char *mydata;
int argc;
char *argv[2];
int recursive = 0;
if (!data || ast_strlen_zero(data))
return NULL;
if (!chan->cdr)
return NULL;
mydata = ast_strdupa(data);
argc = ast_separate_app_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
/* check for a trailing flags argument */
if (argc > 1) {
argc--;
if (strchr(argv[argc], 'r'))
recursive = 1;
}
ast_cdr_getvar(chan->cdr, argv[0], &ret, buf, len, recursive);
return ret;
}
static void builtin_function_cdr_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
{
char *mydata;
int argc;
char *argv[2];
int recursive = 0;
if (!data || ast_strlen_zero(data) || !value)
return;
if (!chan->cdr)
return;
mydata = ast_strdupa(data);
argc = ast_separate_app_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
/* check for a trailing flags argument */
if (argc > 1) {
argc--;
if (strchr(argv[argc], 'r'))
recursive = 1;
}
ast_cdr_setvar(chan->cdr, argv[0], value, recursive);
}
static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0", *ret;
char *arg, *earg, *tmp, errstr[256] = "";
int errcode;
regex_t regexbuf;
ret = ret_false; /* convince me otherwise */
tmp = ast_strdupa(data);
if (tmp) {
/* Regex in quotes */
arg = strchr(tmp, '"');
if (arg) {
arg++;
earg = strrchr(arg, '"');
if (earg) {
*earg = '\0';
}
} else {
arg = tmp;
}
if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
regerror(errcode, &regexbuf, errstr, sizeof(errstr));
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
ret = NULL;
} else {
ret = regexec(&regexbuf, data, 0, NULL, 0) ? ret_false : ret_true;
}
regfree(&regexbuf);
} else {
ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
}
return ret;
}
static char *builtin_function_md5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char md5[33];
if (!data || ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "Syntax: MD5(<data>) - missing argument!\n");
return NULL;
}
ast_md5_hash(md5, data);
ast_copy_string(buf, md5, len);
return buf;
}
static char *builtin_function_checkmd5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
int argc;
char *argv[2];
char *args;
char newmd5[33];
if (!data || ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
return NULL;
}
args = ast_strdupa(data);
argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0]));
if (argc < 2) {
ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
return NULL;
}
ast_md5_hash(newmd5, argv[1]);
if (!strcasecmp(newmd5, argv[0])) /* they match */
ast_copy_string(buf, "1", len);
else
ast_copy_string(buf, "0", len);
return buf;
}
static void pbx_substitute_variables_helper_full(struct ast_channel *c, const char *cp1, char *cp2, int count, struct varshead *headp)
{
char *cp4;
@ -2995,7 +2889,11 @@ static char show_application_help[] =
static char show_functions_help[] =
"Usage: show functions\n"
" List builtin functions accessable as $(function args)";
" List builtin functions accessable as $(function args)\n";
static char show_function_help[] =
"Usage: show function <function>\n"
" Describe a particular dialplan function.\n";
static char show_applications_help[] =
"Usage: show applications [{like|describing} <text>]\n"
@ -3104,8 +3002,8 @@ static int handle_show_application(int fd, int argc, char *argv[])
if (synopsis && description) {
snprintf(info, 64 + AST_MAX_APP, "\n -= Info about application '%s' =- \n\n", a->name);
term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
term_color(syntitle, "[Synopsis]:\n", COLOR_MAGENTA, 0, 40);
term_color(destitle, "[Description]:\n", COLOR_MAGENTA, 0, 40);
term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
term_color(synopsis,
a->synopsis ? a->synopsis : "Not available",
COLOR_CYAN, 0, synopsis_size);
@ -3117,8 +3015,8 @@ static int handle_show_application(int fd, int argc, char *argv[])
} else {
/* ... one of our applications, show info ...*/
ast_cli(fd,"\n -= Info about application '%s' =- \n\n"
"[Synopsis]:\n %s\n\n"
"[Description]:\n%s\n",
"[Synopsis]\n %s\n\n"
"[Description]\n%s\n",
a->name,
a->synopsis ? a->synopsis : "Not available",
a->description ? a->description : "Not available");
@ -3578,81 +3476,6 @@ static int handle_show_dialplan(int fd, int argc, char *argv[])
return RESULT_SUCCESS;
}
/* custom commands */
static struct ast_custom_function_obj regex_function = {
.name = "regex",
.desc = "Regular Expression: Returns 1 if data matches regular expression.",
.syntax = "regex(\"<regular expression>\" <data>)",
.read = builtin_function_regex,
.write = NULL,
};
static struct ast_custom_function_obj isnull_function = {
.name = "isnull",
.desc = "NULL Test: Returns 1 if NULL or 0 otherwise",
.syntax = "isnull(<data>)",
.read = builtin_function_isnull,
.write = NULL,
};
static struct ast_custom_function_obj exists_function = {
.name = "exists",
.desc = "Existence Test: Returns 1 if exists, 0 otherwise",
.syntax = "exists(<data>)",
.read = builtin_function_exists,
.write = NULL,
};
static struct ast_custom_function_obj if_function = {
.name = "if",
.desc = "Conditional: Returns the data following '?' if true else the data following ':'",
.syntax = "if(<expr>?<true>:<false>)",
.read = builtin_function_if,
.write = NULL,
};
static struct ast_custom_function_obj env_function = {
.name = "ENV",
.desc = "Gets or sets the environment variable specified",
.syntax = "ENV(<envname>)",
.read = builtin_function_env_read,
.write = builtin_function_env_write,
};
static struct ast_custom_function_obj len_function = {
.name = "LEN",
.desc = "Returns the length of the arguments given",
.syntax = "LEN(<string>)",
.read = builtin_function_len,
.write = NULL,
};
static struct ast_custom_function_obj cdr_function = {
.name = "CDR",
.desc = "Gets or sets a CDR variable; option 'r' searches the entire stack of CDRs on the channel",
.syntax = "CDR(<name>[|options])",
.read = builtin_function_cdr_read,
.write = builtin_function_cdr_write,
};
static struct ast_custom_function_obj md5_function = {
.name = "MD5",
.desc = "Computes an MD5 digest",
.syntax = "MD5(<data>)",
.read = builtin_function_md5,
.write = NULL,
};
static struct ast_custom_function_obj checkmd5_function = {
.name = "CHECK_MD5",
.desc = "Checks an MD5 digest. Returns 1 on a match, 0 otherwise",
.syntax = "CHECK_MD5(<digest>,<data>)",
.read = builtin_function_checkmd5,
.write = NULL,
};
/*
* CLI entries for upper commands ...
*/
@ -3661,10 +3484,20 @@ static struct ast_cli_entry show_applications_cli =
handle_show_applications, "Shows registered dialplan applications",
show_applications_help, complete_show_applications };
static struct ast_cli_entry show_functions_cli =
{ { "show", "functions", NULL },
handle_show_functions, "Shows registered dialplan functions",
show_functions_help};
static struct ast_cli_entry show_functions_cli = {
{ "show", "functions", NULL },
handle_show_functions,
"Shows registered dialplan functions",
show_functions_help,
};
static struct ast_cli_entry show_function_cli = {
{ "show" , "function", NULL },
handle_show_function,
"Describe a specific dialplan function",
show_function_help,
complete_show_function,
};
static struct ast_cli_entry show_application_cli =
{ { "show", "application", NULL },
@ -6060,20 +5893,12 @@ int load_pbx(void)
}
AST_LIST_HEAD_INIT(&globals);
ast_cli_register(&show_applications_cli);
ast_cli_register(&show_function_cli);
ast_cli_register(&show_functions_cli);
ast_cli_register(&show_application_cli);
ast_cli_register(&show_dialplan_cli);
ast_cli_register(&show_switches_cli);
ast_cli_register(&show_hints_cli);
ast_custom_function_register(&regex_function);
ast_custom_function_register(&isnull_function);
ast_custom_function_register(&exists_function);
ast_custom_function_register(&if_function);
ast_custom_function_register(&env_function);
ast_custom_function_register(&len_function);
ast_custom_function_register(&cdr_function);
ast_custom_function_register(&md5_function);
ast_custom_function_register(&checkmd5_function);
/* Register builtin applications */
for (x=0; x<sizeof(builtins) / sizeof(struct pbx_builtin); x++) {