major dialplan functions update

deprecate LANGUAGE() and MUSICCLASS(), in favor of CHANNEL()


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@9674 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming
2006-02-12 04:28:58 +00:00
parent 3d2404c62c
commit 96bf384e7d
35 changed files with 1160 additions and 1101 deletions

View File

@@ -1,7 +1,7 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2005, Digium, Inc.
* Copyright (C) 2005-2006, Digium, Inc.
* Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
* Portions Copyright (C) 2005, Anthony Minessale II
*
@@ -42,24 +42,19 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/app.h"
#include "asterisk/localtime.h"
static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int function_fieldqty(struct ast_channel *chan, char *cmd,
char *parse, char *buf, size_t len)
{
char *varval, workspace[4096];
char *varval;
int fieldcount = 0;
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(delim);
);
if (!(parse = ast_strdupa(data))) {
ast_copy_string(buf, "0", len);
return buf;
}
AST_APP_ARG(varname);
AST_APP_ARG(delim);
);
AST_STANDARD_APP_ARGS(args, parse);
if (args.delim) {
pbx_retrieve_variable(chan, args.varname, &varval, workspace, sizeof(workspace), NULL);
pbx_retrieve_variable(chan, args.varname, &varval, buf, len, NULL);
while (strsep(&varval, args.delim))
fieldcount++;
} else {
@@ -67,7 +62,7 @@ static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data,
}
snprintf(buf, len, "%d", fieldcount);
return buf;
return 0;
}
static struct ast_custom_function fieldqty_function = {
@@ -77,34 +72,29 @@ static struct ast_custom_function fieldqty_function = {
.read = function_fieldqty,
};
static char *filter(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int filter(struct ast_channel *chan, char *cmd, char *parse, char *buf,
size_t len)
{
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(allowed);
AST_APP_ARG(string);
AST_APP_ARG(allowed);
AST_APP_ARG(string);
);
char *outbuf=buf;
if (!(parse = ast_strdupa(data)))
return "";
char *outbuf = buf;
AST_STANDARD_APP_ARGS(args, parse);
if (!args.string ) {
if (!args.string) {
ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
return "";
return -1;
}
for ( ; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
if (strchr(args.allowed, *(args.string))) {
*outbuf = *(args.string);
outbuf++;
}
for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
if (strchr(args.allowed, *(args.string)))
*outbuf++ = *(args.string);
}
*outbuf = '\0';
return buf;
return 0;
}
static struct ast_custom_function filter_function = {
@@ -114,63 +104,59 @@ static struct ast_custom_function filter_function = {
.read = filter,
};
static char *regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int regex(struct ast_channel *chan, char *cmd, char *parse, char *buf,
size_t len)
{
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(null);
AST_APP_ARG(reg);
AST_APP_ARG(str);
AST_APP_ARG(null);
AST_APP_ARG(reg);
AST_APP_ARG(str);
);
char errstr[256] = "";
int errcode;
regex_t regexbuf;
ast_copy_string(buf, "0", len);
if (!(parse = ast_strdupa(data)))
return buf;
buf[0] = '\0';
AST_NONSTANDARD_APP_ARGS(args, parse, '"');
ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
if ((errcode = regcomp(&regexbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
regerror(errcode, &regexbuf, errstr, sizeof(errstr));
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
regerror(errcode, &regexbuf, buf, len);
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
return -1;
} else {
if (!regexec(&regexbuf, args.str, 0, NULL, 0))
ast_copy_string(buf, "1", len);
strcpy(buf, "1");
}
regfree(&regexbuf);
return buf;
return 0;
}
static struct ast_custom_function regex_function = {
.name = "REGEX",
.synopsis = "Regular Expression: Returns 1 if data matches regular expression.",
.synopsis =
"Regular Expression: Returns 1 if data matches regular expression.",
.syntax = "REGEX(\"<regular expression>\" <data>)",
.read = regex,
};
static void array(struct ast_channel *chan, char *cmd, char *data, const char *value)
static int array(struct ast_channel *chan, char *cmd, char *var,
const char *value)
{
AST_DECLARE_APP_ARGS(arg1,
AST_APP_ARG(var)[100];
AST_APP_ARG(var)[100];
);
AST_DECLARE_APP_ARGS(arg2,
AST_APP_ARG(val)[100];
AST_APP_ARG(val)[100];
);
char *var, *value2;
char *value2;
int i;
var = ast_strdupa(data);
value2 = ast_strdupa(value);
if (!var || !value2)
return;
return -1;
/* The functions this will generally be used with are SORT and ODBC_*, which
* both return comma-delimited lists. However, if somebody uses literal lists,
@@ -179,20 +165,19 @@ static void array(struct ast_channel *chan, char *cmd, char *data, const char *v
* delimiter, but we'll fall back to vertical bars if commas aren't found.
*/
ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
if (strchr(var, ',')) {
if (strchr(var, ','))
AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
} else {
else
AST_STANDARD_APP_ARGS(arg1, var);
}
if (strchr(value2, ',')) {
if (strchr(value2, ','))
AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
} else {
else
AST_STANDARD_APP_ARGS(arg2, value2);
}
for (i = 0; i < arg1.argc; i++) {
ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i], arg2.val[i]);
ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
arg2.val[i]);
if (i < arg2.argc) {
pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
} else {
@@ -201,6 +186,8 @@ static void array(struct ast_channel *chan, char *cmd, char *data, const char *v
pbx_builtin_setvar_helper(chan, arg1.var[i], "");
}
}
return 0;
}
static struct ast_custom_function array_function = {
@@ -209,22 +196,25 @@ static struct ast_custom_function array_function = {
.syntax = "ARRAY(var1[,var2[...][,varN]])",
.write = array,
.desc =
"The comma-separated list passed as a value to which the function is set will\n"
"be interpreted as a set of values to which the comma-separated list of\n"
"variable names in the argument should be set.\n"
"Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2\n"
"Note: remember to either backslash your commas in extensions.conf or quote the\n"
"entire argument, since Set can take multiple arguments itself.\n",
"The comma-separated list passed as a value to which the function is set will\n"
"be interpreted as a set of values to which the comma-separated list of\n"
"variable names in the argument should be set.\n"
"Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2\n"
"Note: remember to either backslash your commas in extensions.conf or quote the\n"
"entire argument, since Set can take multiple arguments itself.\n",
};
static char *len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, char *cmd, char *data, char *buf,
size_t len)
{
int length = 0;
if (data) {
if (data)
length = strlen(data);
}
snprintf(buf, len, "%d", length);
return buf;
return 0;
}
static struct ast_custom_function len_function = {
@@ -234,27 +224,25 @@ static struct ast_custom_function len_function = {
.read = len,
};
static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
char *buf, size_t len)
{
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(epoch);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
AST_APP_ARG(epoch);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
);
long epochi;
struct tm time;
buf[0] = '\0';
if (ast_strlen_zero(data)) {
ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
return buf;
if (ast_strlen_zero(parse)) {
ast_log(LOG_ERROR,
"Asterisk function STRFTIME() requires an argument.\n");
return -1;
}
if (!(parse = ast_strdupa(data)))
return buf;
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.epoch) || !sscanf(args.epoch, "%ld", &epochi)) {
@@ -264,12 +252,12 @@ static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char
ast_localtime(&epochi, &time, args.timezone);
if (!strftime(buf, len, args.format?args.format:"%c", &time)) {
if (!strftime(buf, len, args.format ? args.format : "%c", &time))
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
}
buf[len - 1] = '\0';
return buf;
return 0;
}
static struct ast_custom_function strftime_function = {
@@ -279,66 +267,71 @@ static struct ast_custom_function strftime_function = {
.read = acf_strftime,
};
static char *acf_strptime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
char *buf, size_t len)
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(timestring);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
AST_APP_ARG(timestring);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
);
struct tm time;
memset(&time, 0, sizeof(struct tm));
buf[0] = '\0';
if (!data) {
ast_log(LOG_ERROR, "Asterisk function STRPTIME() requires an argument.\n");
return buf;
ast_log(LOG_ERROR,
"Asterisk function STRPTIME() requires an argument.\n");
return -1;
}
AST_STANDARD_APP_ARGS(args, data);
if (ast_strlen_zero(args.format) ) {
ast_log(LOG_ERROR, "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
return buf;
if (ast_strlen_zero(args.format)) {
ast_log(LOG_ERROR,
"No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
return -1;
}
if (!strptime(args.timestring, args.format, &time)) {
ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
} else {
snprintf(buf, len, "%d", (int)ast_mktime(&time, args.timezone));
snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
}
return buf;
return 0;
}
static struct ast_custom_function strptime_function = {
.name = "STRPTIME",
.synopsis = "Returns the epoch of the arbitrary date/time string structured as described in the format.",
.synopsis =
"Returns the epoch of the arbitrary date/time string structured as described in the format.",
.syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
.desc =
"This is useful for converting a date into an EPOCH time, possibly to pass to\n"
"an application like SayUnixTime or to calculate the difference between two\n"
"date strings.\n"
"\n"
"Example:\n"
" ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
"This is useful for converting a date into an EPOCH time, possibly to pass to\n"
"an application like SayUnixTime or to calculate the difference between two\n"
"date strings.\n"
"\n"
"Example:\n"
" ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
.read = acf_strptime,
};
static char *function_eval(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int function_eval(struct ast_channel *chan, char *cmd, char *data,
char *buf, size_t len)
{
memset(buf, 0, len);
buf[0] = '\0';
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
return buf;
return -1;
}
pbx_substitute_variables_helper(chan, data, buf, len - 1);
return buf;
return 0;
}
static struct ast_custom_function eval_function = {
@@ -353,7 +346,7 @@ static struct ast_custom_function eval_function = {
"contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
"in the dialplan will be the contents of the variable, OTHERVAR.\n"
"Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
"left with \"${OTHERVAR}\".\n",
"left with \"${OTHERVAR}\".\n",
.read = function_eval,
};
@@ -405,11 +398,3 @@ char *key()
{
return ASTERISK_GPL_KEY;
}
/*
Local Variables:
mode: C
c-file-style: "linux"
indent-tabs-mode: nil
End:
*/