2005-05-05 05:39:33 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-05-05 05:39:33 +00:00
|
|
|
*
|
2006-02-12 04:28:58 +00:00
|
|
|
* Copyright (C) 2005-2006, Digium, Inc.
|
2005-05-05 12:48:52 +00:00
|
|
|
* Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
|
|
|
|
* Portions Copyright (C) 2005, Anthony Minessale II
|
2005-05-05 05:39:33 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
2005-05-05 05:39:33 +00:00
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief String manipulation dialplan functions
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Tilghman Lesher
|
|
|
|
* \author Anothony Minessale II
|
2005-05-05 05:39:33 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2005-05-05 05:39:33 +00:00
|
|
|
#include <stdlib.h>
|
2005-11-08 04:48:00 +00:00
|
|
|
#include <stdio.h>
|
2005-05-05 05:39:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
#include "asterisk/module.h"
|
2005-05-05 05:39:33 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/app.h"
|
2005-05-05 05:50:11 +00:00
|
|
|
#include "asterisk/localtime.h"
|
2006-10-03 15:53:07 +00:00
|
|
|
#include "asterisk/options.h"
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int function_fieldqty(struct ast_channel *chan, char *cmd,
|
|
|
|
char *parse, char *buf, size_t len)
|
2005-05-05 05:39:33 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
char *varval;
|
2005-05-05 05:39:33 +00:00
|
|
|
int fieldcount = 0;
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(varname);
|
|
|
|
AST_APP_ARG(delim);
|
|
|
|
);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
|
|
|
if (args.delim) {
|
2006-02-12 04:28:58 +00:00
|
|
|
pbx_retrieve_variable(chan, args.varname, &varval, buf, len, NULL);
|
2006-01-14 19:30:37 +00:00
|
|
|
while (strsep(&varval, args.delim))
|
|
|
|
fieldcount++;
|
2005-05-05 05:39:33 +00:00
|
|
|
} else {
|
2006-01-14 19:30:37 +00:00
|
|
|
fieldcount = 1;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
2006-01-14 19:30:37 +00:00
|
|
|
snprintf(buf, len, "%d", fieldcount);
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
return 0;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function fieldqty_function = {
|
2005-05-05 05:39:33 +00:00
|
|
|
.name = "FIELDQTY",
|
|
|
|
.synopsis = "Count the fields, with an arbitrary delimiter",
|
2006-07-01 16:03:51 +00:00
|
|
|
.syntax = "FIELDQTY(<varname>|<delim>)",
|
2005-05-05 05:39:33 +00:00
|
|
|
.read = function_fieldqty,
|
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int filter(struct ast_channel *chan, char *cmd, char *parse, char *buf,
|
|
|
|
size_t len)
|
2005-12-23 21:03:25 +00:00
|
|
|
{
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(allowed);
|
|
|
|
AST_APP_ARG(string);
|
2006-01-14 19:30:37 +00:00
|
|
|
);
|
2006-02-12 04:28:58 +00:00
|
|
|
char *outbuf = buf;
|
2005-12-23 21:03:25 +00:00
|
|
|
|
2006-07-01 16:03:51 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
2005-12-23 21:03:25 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
if (!args.string) {
|
2006-07-01 16:03:51 +00:00
|
|
|
ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>|<string>)\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-12-23 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
|
|
|
|
if (strchr(args.allowed, *(args.string)))
|
|
|
|
*outbuf++ = *(args.string);
|
2005-12-23 21:03:25 +00:00
|
|
|
}
|
|
|
|
*outbuf = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-12-23 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function filter_function = {
|
2005-12-23 21:03:25 +00:00
|
|
|
.name = "FILTER",
|
|
|
|
.synopsis = "Filter the string to include only the allowed characters",
|
2006-07-01 16:03:51 +00:00
|
|
|
.syntax = "FILTER(<allowed-chars>|<string>)",
|
2006-02-11 03:14:05 +00:00
|
|
|
.read = filter,
|
2005-12-23 21:03:25 +00:00
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int regex(struct ast_channel *chan, char *cmd, char *parse, char *buf,
|
|
|
|
size_t len)
|
2005-05-05 05:39:33 +00:00
|
|
|
{
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(null);
|
|
|
|
AST_APP_ARG(reg);
|
|
|
|
AST_APP_ARG(str);
|
2006-01-14 19:30:37 +00:00
|
|
|
);
|
2005-05-05 05:39:33 +00:00
|
|
|
int errcode;
|
|
|
|
regex_t regexbuf;
|
|
|
|
|
2006-08-31 04:19:54 +00:00
|
|
|
buf[0] = '\0';
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_NONSTANDARD_APP_ARGS(args, parse, '"');
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-07-03 06:32:35 +00:00
|
|
|
if (args.argc != 3) {
|
|
|
|
ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
|
|
|
|
return -1;
|
|
|
|
}
|
As per discussion on bug 7862, the problem wasn't the fact that the documentation differed from behavior, but rather that users are used to REGEX having that space after the double quote in 1.2.x. So, in keeping with history, I investigated a little deeper, and discovered that the change in behavior was due to the modification of the function to use the AST_DECLARE_APP_ARGS and AST_NONSTANDARD_APP_ARGS() to parse the args. The code to skip the blank was left out. So, what I did was add code to throw out the first blank (space or tab) after the double quote, IF IT IS THERE. If not, nothing is done.Verbage is added to the function description saying that the space is optional, and skipped if it is there. If a space is desired, then the documentation advises putting two spaces there. This should make it compatible for 1.2 users, and not mess up new users who are used to using it with no space. It WILL mess up new users who WANT a space. Hopefully, they will double check the doc strings for this func and add the extra space. Hopefully, this class of new user is very small.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@42423 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-09-08 16:44:38 +00:00
|
|
|
if ((*args.str == ' ') || (*args.str == '\t'))
|
|
|
|
args.str++;
|
2006-07-03 06:32:35 +00:00
|
|
|
|
2006-10-03 15:53:07 +00:00
|
|
|
if (option_debug)
|
|
|
|
ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
|
2006-01-14 19:30:37 +00:00
|
|
|
|
|
|
|
if ((errcode = regcomp(®exbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
|
2006-02-12 04:28:58 +00:00
|
|
|
regerror(errcode, ®exbuf, buf, len);
|
|
|
|
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
|
|
|
|
return -1;
|
2005-10-12 01:09:04 +00:00
|
|
|
}
|
2006-08-31 04:07:35 +00:00
|
|
|
|
|
|
|
strcpy(buf, regexec(®exbuf, args.str, 0, NULL, 0) ? "0" : "1");
|
|
|
|
|
2005-10-12 01:09:04 +00:00
|
|
|
regfree(®exbuf);
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
return 0;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function regex_function = {
|
2005-05-05 05:39:33 +00:00
|
|
|
.name = "REGEX",
|
2006-08-31 04:07:35 +00:00
|
|
|
.synopsis = "Regular Expression",
|
2006-09-07 04:23:00 +00:00
|
|
|
.desc =
|
|
|
|
"Returns 1 if data matches regular expression, or 0 otherwise.\n"
|
As per discussion on bug 7862, the problem wasn't the fact that the documentation differed from behavior, but rather that users are used to REGEX having that space after the double quote in 1.2.x. So, in keeping with history, I investigated a little deeper, and discovered that the change in behavior was due to the modification of the function to use the AST_DECLARE_APP_ARGS and AST_NONSTANDARD_APP_ARGS() to parse the args. The code to skip the blank was left out. So, what I did was add code to throw out the first blank (space or tab) after the double quote, IF IT IS THERE. If not, nothing is done.Verbage is added to the function description saying that the space is optional, and skipped if it is there. If a space is desired, then the documentation advises putting two spaces there. This should make it compatible for 1.2 users, and not mess up new users who are used to using it with no space. It WILL mess up new users who WANT a space. Hopefully, they will double check the doc strings for this func and add the extra space. Hopefully, this class of new user is very small.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@42423 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-09-08 16:44:38 +00:00
|
|
|
"Please note that the space following the double quotes separating the regex from the data\n"
|
|
|
|
"is optional and if present, is skipped. If a space is desired at the beginning of the data,\n"
|
|
|
|
"then put two spaces there; the second will not be skipped.\n",
|
|
|
|
.syntax = "REGEX(\"<regular expression>\" <data>)",
|
2006-02-11 03:14:05 +00:00
|
|
|
.read = regex,
|
2005-05-05 05:39:33 +00:00
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int array(struct ast_channel *chan, char *cmd, char *var,
|
|
|
|
const char *value)
|
2005-12-27 06:50:25 +00:00
|
|
|
{
|
2006-01-16 17:31:30 +00:00
|
|
|
AST_DECLARE_APP_ARGS(arg1,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(var)[100];
|
2006-01-16 17:31:30 +00:00
|
|
|
);
|
|
|
|
AST_DECLARE_APP_ARGS(arg2,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(val)[100];
|
2006-01-16 17:31:30 +00:00
|
|
|
);
|
2006-02-12 04:28:58 +00:00
|
|
|
char *value2;
|
2006-01-16 17:31:30 +00:00
|
|
|
int i;
|
2005-12-27 06:50:25 +00:00
|
|
|
|
|
|
|
value2 = ast_strdupa(value);
|
2006-01-21 20:57:06 +00:00
|
|
|
if (!var || !value2)
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-12-27 06:50:25 +00:00
|
|
|
|
|
|
|
/* The functions this will generally be used with are SORT and ODBC_*, which
|
|
|
|
* both return comma-delimited lists. However, if somebody uses literal lists,
|
|
|
|
* their commas will be translated to vertical bars by the load, and I don't
|
|
|
|
* want them to be surprised by the result. Hence, we prefer commas as the
|
|
|
|
* delimiter, but we'll fall back to vertical bars if commas aren't found.
|
|
|
|
*/
|
2006-10-03 15:53:07 +00:00
|
|
|
if (option_debug)
|
|
|
|
ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
|
2006-02-12 04:28:58 +00:00
|
|
|
if (strchr(var, ','))
|
2006-01-16 17:31:30 +00:00
|
|
|
AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
|
2006-02-12 04:28:58 +00:00
|
|
|
else
|
2006-01-16 17:31:30 +00:00
|
|
|
AST_STANDARD_APP_ARGS(arg1, var);
|
2005-12-27 06:50:25 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
if (strchr(value2, ','))
|
2006-01-16 17:31:30 +00:00
|
|
|
AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
|
2006-02-12 04:28:58 +00:00
|
|
|
else
|
2006-01-16 17:31:30 +00:00
|
|
|
AST_STANDARD_APP_ARGS(arg2, value2);
|
2005-12-27 06:50:25 +00:00
|
|
|
|
2006-01-16 17:31:30 +00:00
|
|
|
for (i = 0; i < arg1.argc; i++) {
|
2006-10-03 15:53:07 +00:00
|
|
|
if (option_debug)
|
|
|
|
ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
|
|
|
|
arg2.val[i]);
|
2006-01-16 17:31:30 +00:00
|
|
|
if (i < arg2.argc) {
|
|
|
|
pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
|
2005-12-27 06:50:25 +00:00
|
|
|
} else {
|
|
|
|
/* We could unset the variable, by passing a NULL, but due to
|
|
|
|
* pushvar semantics, that could create some undesired behavior. */
|
2006-01-16 17:31:30 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, arg1.var[i], "");
|
2005-12-27 06:50:25 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-12-27 06:50:25 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function array_function = {
|
2005-12-27 06:50:25 +00:00
|
|
|
.name = "ARRAY",
|
|
|
|
.synopsis = "Allows setting multiple variables at once",
|
2006-07-01 16:03:51 +00:00
|
|
|
.syntax = "ARRAY(var1[|var2[...][|varN]])",
|
2006-02-11 03:14:05 +00:00
|
|
|
.write = array,
|
2005-12-27 06:50:25 +00:00
|
|
|
.desc =
|
2006-02-12 04:28:58 +00:00
|
|
|
"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"
|
2006-07-01 16:03:51 +00:00
|
|
|
"Hence, Set(ARRAY(var1|var2)=1\\,2) will set var1 to 1 and var2 to 2\n"
|
2006-02-12 04:28:58 +00:00
|
|
|
"Note: remember to either backslash your commas in extensions.conf or quote the\n"
|
|
|
|
"entire argument, since Set can take multiple arguments itself.\n",
|
2005-12-27 06:50:25 +00:00
|
|
|
};
|
|
|
|
|
2006-05-18 00:00:32 +00:00
|
|
|
static int acf_sprintf(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
#define SPRINTF_FLAG 0
|
|
|
|
#define SPRINTF_WIDTH 1
|
|
|
|
#define SPRINTF_PRECISION 2
|
|
|
|
#define SPRINTF_LENGTH 3
|
|
|
|
#define SPRINTF_CONVERSION 4
|
|
|
|
int i, state = -1, argcount = 0;
|
|
|
|
char *formatstart = NULL, *bufptr = buf;
|
|
|
|
char formatbuf[256] = "";
|
|
|
|
int tmpi;
|
|
|
|
double tmpd;
|
|
|
|
AST_DECLARE_APP_ARGS(arg,
|
|
|
|
AST_APP_ARG(format);
|
|
|
|
AST_APP_ARG(var)[100];
|
|
|
|
);
|
|
|
|
|
|
|
|
AST_STANDARD_APP_ARGS(arg, data);
|
|
|
|
|
|
|
|
/* Scan the format, converting each argument into the requisite format type. */
|
|
|
|
for (i = 0; arg.format[i]; i++) {
|
|
|
|
switch (state) {
|
|
|
|
case SPRINTF_FLAG:
|
|
|
|
if (strchr("#0- +'I", arg.format[i]))
|
|
|
|
break;
|
|
|
|
state = SPRINTF_WIDTH;
|
|
|
|
case SPRINTF_WIDTH:
|
|
|
|
if (arg.format[i] >= '0' && arg.format[i] <= '9')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Next character must be a period to go into a precision */
|
|
|
|
if (arg.format[i] == '.') {
|
|
|
|
state = SPRINTF_PRECISION;
|
|
|
|
} else {
|
|
|
|
state = SPRINTF_LENGTH;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPRINTF_PRECISION:
|
|
|
|
if (arg.format[i] >= '0' && arg.format[i] <= '9')
|
|
|
|
break;
|
|
|
|
state = SPRINTF_LENGTH;
|
|
|
|
case SPRINTF_LENGTH:
|
|
|
|
if (strchr("hl", arg.format[i])) {
|
|
|
|
if (arg.format[i + 1] == arg.format[i])
|
|
|
|
i++;
|
|
|
|
state = SPRINTF_CONVERSION;
|
|
|
|
break;
|
|
|
|
} else if (strchr("Lqjzt", arg.format[i]))
|
|
|
|
state = SPRINTF_CONVERSION;
|
|
|
|
break;
|
|
|
|
state = SPRINTF_CONVERSION;
|
|
|
|
case SPRINTF_CONVERSION:
|
|
|
|
if (strchr("diouxXc", arg.format[i])) {
|
|
|
|
/* Integer */
|
|
|
|
|
|
|
|
/* Isolate this format alone */
|
|
|
|
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
|
|
|
|
formatbuf[&arg.format[i] - formatstart + 1] = '\0';
|
|
|
|
|
|
|
|
/* Convert the argument into the required type */
|
|
|
|
if (sscanf(arg.var[argcount++], "%i", &tmpi) != 1) {
|
|
|
|
ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf);
|
|
|
|
goto sprintf_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the argument */
|
|
|
|
snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi);
|
|
|
|
|
|
|
|
/* Update the position of the next parameter to print */
|
|
|
|
bufptr = strchr(buf, '\0');
|
|
|
|
} else if (strchr("eEfFgGaA", arg.format[i])) {
|
|
|
|
/* Double */
|
|
|
|
|
|
|
|
/* Isolate this format alone */
|
|
|
|
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
|
|
|
|
formatbuf[&arg.format[i] - formatstart + 1] = '\0';
|
|
|
|
|
|
|
|
/* Convert the argument into the required type */
|
|
|
|
if (sscanf(arg.var[argcount++], "%lf", &tmpd) != 1) {
|
|
|
|
ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf);
|
|
|
|
goto sprintf_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the argument */
|
|
|
|
snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd);
|
|
|
|
|
|
|
|
/* Update the position of the next parameter to print */
|
|
|
|
bufptr = strchr(buf, '\0');
|
|
|
|
} else if (arg.format[i] == 's') {
|
|
|
|
/* String */
|
|
|
|
|
|
|
|
/* Isolate this format alone */
|
|
|
|
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
|
|
|
|
formatbuf[&arg.format[i] - formatstart + 1] = '\0';
|
|
|
|
|
|
|
|
/* Format the argument */
|
|
|
|
snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]);
|
|
|
|
|
|
|
|
/* Update the position of the next parameter to print */
|
|
|
|
bufptr = strchr(buf, '\0');
|
|
|
|
} else if (arg.format[i] == '%') {
|
|
|
|
/* Literal data to copy */
|
|
|
|
*bufptr++ = arg.format[i];
|
|
|
|
} else {
|
|
|
|
/* Not supported */
|
|
|
|
|
|
|
|
/* Isolate this format alone */
|
|
|
|
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
|
|
|
|
formatbuf[&arg.format[i] - formatstart + 1] = '\0';
|
|
|
|
|
|
|
|
ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]);
|
|
|
|
goto sprintf_fail;
|
|
|
|
}
|
|
|
|
state = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (arg.format[i] == '%') {
|
|
|
|
state = SPRINTF_FLAG;
|
|
|
|
formatstart = &arg.format[i];
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Literal data to copy */
|
|
|
|
*bufptr++ = arg.format[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
sprintf_fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_custom_function sprintf_function = {
|
|
|
|
.name = "SPRINTF",
|
|
|
|
.synopsis = "Format a variable according to a format string",
|
2006-07-01 16:03:51 +00:00
|
|
|
.syntax = "SPRINTF(<format>|<arg1>[|...<argN>])",
|
2006-05-18 00:00:32 +00:00
|
|
|
.read = acf_sprintf,
|
|
|
|
.desc =
|
|
|
|
"Parses the format string specified and returns a string matching that format.\n"
|
|
|
|
"Supports most options supported by sprintf(3). Returns a shortened string if\n"
|
|
|
|
"a format specifier is not recognized.\n",
|
|
|
|
};
|
|
|
|
|
2006-02-23 23:36:53 +00:00
|
|
|
static int quote(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
|
2006-02-23 22:59:16 +00:00
|
|
|
{
|
|
|
|
char *bufptr = buf, *dataptr = data;
|
|
|
|
*bufptr++ = '"';
|
|
|
|
for (; bufptr < buf + len - 1; dataptr++) {
|
|
|
|
if (*dataptr == '\\') {
|
|
|
|
*bufptr++ = '\\';
|
|
|
|
*bufptr++ = '\\';
|
|
|
|
} else if (*dataptr == '"') {
|
|
|
|
*bufptr++ = '\\';
|
|
|
|
*bufptr++ = '"';
|
|
|
|
} else if (*dataptr == '\0') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
*bufptr++ = *dataptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufptr++ = '"';
|
|
|
|
*bufptr = '\0';
|
2006-02-23 23:36:53 +00:00
|
|
|
return 0;
|
2006-02-23 22:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_custom_function quote_function = {
|
|
|
|
.name = "QUOTE",
|
|
|
|
.synopsis = "Quotes a given string, escaping embedded quotes as necessary",
|
|
|
|
.syntax = "QUOTE(<string>)",
|
2006-02-23 23:36:53 +00:00
|
|
|
.read = quote,
|
2006-02-23 22:59:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int len(struct ast_channel *chan, char *cmd, char *data, char *buf,
|
|
|
|
size_t len)
|
2005-05-05 05:39:33 +00:00
|
|
|
{
|
|
|
|
int length = 0;
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
if (data)
|
2005-05-05 05:39:33 +00:00
|
|
|
length = strlen(data);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2005-05-05 05:39:33 +00:00
|
|
|
snprintf(buf, len, "%d", length);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function len_function = {
|
2005-05-05 05:39:33 +00:00
|
|
|
.name = "LEN",
|
|
|
|
.synopsis = "Returns the length of the argument given",
|
|
|
|
.syntax = "LEN(<string>)",
|
2006-02-11 03:14:05 +00:00
|
|
|
.read = len,
|
2005-05-05 05:39:33 +00:00
|
|
|
};
|
2005-05-05 05:50:11 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
|
|
|
|
char *buf, size_t len)
|
2005-05-05 05:50:11 +00:00
|
|
|
{
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(epoch);
|
|
|
|
AST_APP_ARG(timezone);
|
|
|
|
AST_APP_ARG(format);
|
2006-01-14 19:30:37 +00:00
|
|
|
);
|
2006-02-15 00:24:24 +00:00
|
|
|
time_t epochi;
|
|
|
|
struct tm tm;
|
2005-05-05 05:50:11 +00:00
|
|
|
|
2005-10-17 17:54:32 +00:00
|
|
|
buf[0] = '\0';
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
if (ast_strlen_zero(parse)) {
|
|
|
|
ast_log(LOG_ERROR,
|
|
|
|
"Asterisk function STRFTIME() requires an argument.\n");
|
|
|
|
return -1;
|
2005-05-05 05:50:11 +00:00
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-01-14 19:30:37 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
2005-10-17 17:54:32 +00:00
|
|
|
|
2006-02-23 17:13:57 +00:00
|
|
|
ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
|
2006-02-15 00:24:24 +00:00
|
|
|
ast_localtime(&epochi, &tm, args.timezone);
|
2005-10-17 17:54:32 +00:00
|
|
|
|
2006-02-15 00:24:24 +00:00
|
|
|
if (!args.format)
|
|
|
|
args.format = "%c";
|
2005-10-17 17:54:32 +00:00
|
|
|
|
2006-02-15 00:24:24 +00:00
|
|
|
if (!strftime(buf, len, args.format, &tm))
|
2005-10-17 17:54:32 +00:00
|
|
|
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2005-10-17 17:54:32 +00:00
|
|
|
buf[len - 1] = '\0';
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
return 0;
|
2005-05-05 05:50:11 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function strftime_function = {
|
2005-05-05 05:50:11 +00:00
|
|
|
.name = "STRFTIME",
|
|
|
|
.synopsis = "Returns the current date/time in a specified format.",
|
2006-07-01 16:03:51 +00:00
|
|
|
.syntax = "STRFTIME([<epoch>][|[timezone][|format]])",
|
2005-05-05 05:50:11 +00:00
|
|
|
.read = acf_strftime,
|
|
|
|
};
|
2005-05-15 23:53:11 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
|
|
|
|
char *buf, size_t len)
|
2006-01-22 08:09:02 +00:00
|
|
|
{
|
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(timestring);
|
|
|
|
AST_APP_ARG(timezone);
|
|
|
|
AST_APP_ARG(format);
|
2006-01-22 08:09:02 +00:00
|
|
|
);
|
|
|
|
struct tm time;
|
|
|
|
|
|
|
|
memset(&time, 0, sizeof(struct tm));
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-01-22 08:09:02 +00:00
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
if (!data) {
|
2006-02-12 04:28:58 +00:00
|
|
|
ast_log(LOG_ERROR,
|
|
|
|
"Asterisk function STRPTIME() requires an argument.\n");
|
|
|
|
return -1;
|
2006-01-22 08:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AST_STANDARD_APP_ARGS(args, data);
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
if (ast_strlen_zero(args.format)) {
|
|
|
|
ast_log(LOG_ERROR,
|
|
|
|
"No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
|
|
|
|
return -1;
|
2006-01-22 08:09:02 +00:00
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-01-22 08:09:02 +00:00
|
|
|
if (!strptime(args.timestring, args.format, &time)) {
|
|
|
|
ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
|
|
|
|
} else {
|
2006-02-12 04:28:58 +00:00
|
|
|
snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
|
2006-01-22 08:09:02 +00:00
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-01-22 08:09:02 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function strptime_function = {
|
2006-01-22 08:09:02 +00:00
|
|
|
.name = "STRPTIME",
|
2006-02-12 04:28:58 +00:00
|
|
|
.synopsis =
|
|
|
|
"Returns the epoch of the arbitrary date/time string structured as described in the format.",
|
2006-01-22 08:09:02 +00:00
|
|
|
.syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
|
|
|
|
.desc =
|
2006-02-12 04:28:58 +00:00
|
|
|
"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",
|
2006-01-22 08:09:02 +00:00
|
|
|
.read = acf_strptime,
|
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
static int function_eval(struct ast_channel *chan, char *cmd, char *data,
|
|
|
|
char *buf, size_t len)
|
2005-05-15 23:53:11 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
buf[0] = '\0';
|
2005-09-25 20:47:00 +00:00
|
|
|
|
2005-11-08 01:55:31 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2005-09-25 20:47:00 +00:00
|
|
|
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-05-15 23:53:11 +00:00
|
|
|
}
|
2005-09-25 20:47:00 +00:00
|
|
|
|
2005-05-15 23:53:11 +00:00
|
|
|
pbx_substitute_variables_helper(chan, data, buf, len - 1);
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
return 0;
|
2005-05-15 23:53:11 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function eval_function = {
|
2005-05-15 23:53:11 +00:00
|
|
|
.name = "EVAL",
|
|
|
|
.synopsis = "Evaluate stored variables.",
|
|
|
|
.syntax = "EVAL(<variable>)",
|
|
|
|
.desc = "Using EVAL basically causes a string to be evaluated twice.\n"
|
|
|
|
"When a variable or expression is in the dialplan, it will be\n"
|
|
|
|
"evaluated at runtime. However, if the result of the evaluation\n"
|
|
|
|
"is in fact a variable or expression, using EVAL will have it\n"
|
|
|
|
"evaluated a second time. For example, if the variable ${MYVAR}\n"
|
|
|
|
"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"
|
2006-02-12 04:28:58 +00:00
|
|
|
"left with \"${OTHERVAR}\".\n",
|
2005-05-15 23:53:11 +00:00
|
|
|
.read = function_eval,
|
|
|
|
};
|
|
|
|
|
2006-02-15 00:59:01 +00:00
|
|
|
static int keypadhash(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
char *bufptr, *dataptr;
|
|
|
|
|
|
|
|
for (bufptr = buf, dataptr = data; bufptr < buf + len - 1; dataptr++) {
|
|
|
|
if (*dataptr == '1') {
|
|
|
|
*bufptr++ = '1';
|
|
|
|
} else if (strchr("AaBbCc2", *dataptr)) {
|
|
|
|
*bufptr++ = '2';
|
|
|
|
} else if (strchr("DdEeFf3", *dataptr)) {
|
|
|
|
*bufptr++ = '3';
|
|
|
|
} else if (strchr("GgHhIi4", *dataptr)) {
|
|
|
|
*bufptr++ = '4';
|
|
|
|
} else if (strchr("JjKkLl5", *dataptr)) {
|
|
|
|
*bufptr++ = '5';
|
|
|
|
} else if (strchr("MmNnOo6", *dataptr)) {
|
|
|
|
*bufptr++ = '6';
|
|
|
|
} else if (strchr("PpQqRrSs7", *dataptr)) {
|
|
|
|
*bufptr++ = '7';
|
|
|
|
} else if (strchr("TtUuVv8", *dataptr)) {
|
|
|
|
*bufptr++ = '8';
|
|
|
|
} else if (strchr("WwXxYyZz9", *dataptr)) {
|
|
|
|
*bufptr++ = '9';
|
|
|
|
} else if (*dataptr == '0') {
|
|
|
|
*bufptr++ = '0';
|
|
|
|
} else if (*dataptr == '\0') {
|
|
|
|
*bufptr++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_custom_function keypadhash_function = {
|
|
|
|
.name = "KEYPADHASH",
|
|
|
|
.synopsis = "Hash the letters in the string into the equivalent keypad numbers.",
|
|
|
|
.syntax = "KEYPADHASH(<string>)",
|
|
|
|
.read = keypadhash,
|
|
|
|
.desc = "Example: ${KEYPADHASH(Les)} returns \"537\"\n",
|
|
|
|
};
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
res |= ast_custom_function_unregister(&fieldqty_function);
|
|
|
|
res |= ast_custom_function_unregister(&filter_function);
|
|
|
|
res |= ast_custom_function_unregister(®ex_function);
|
|
|
|
res |= ast_custom_function_unregister(&array_function);
|
2006-02-23 22:59:16 +00:00
|
|
|
res |= ast_custom_function_unregister("e_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
res |= ast_custom_function_unregister(&len_function);
|
|
|
|
res |= ast_custom_function_unregister(&strftime_function);
|
|
|
|
res |= ast_custom_function_unregister(&strptime_function);
|
|
|
|
res |= ast_custom_function_unregister(&eval_function);
|
2006-02-15 00:59:01 +00:00
|
|
|
res |= ast_custom_function_unregister(&keypadhash_function);
|
2006-05-18 00:00:32 +00:00
|
|
|
res |= ast_custom_function_unregister(&sprintf_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
res |= ast_custom_function_register(&fieldqty_function);
|
|
|
|
res |= ast_custom_function_register(&filter_function);
|
|
|
|
res |= ast_custom_function_register(®ex_function);
|
|
|
|
res |= ast_custom_function_register(&array_function);
|
2006-02-23 22:59:16 +00:00
|
|
|
res |= ast_custom_function_register("e_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
res |= ast_custom_function_register(&len_function);
|
|
|
|
res |= ast_custom_function_register(&strftime_function);
|
|
|
|
res |= ast_custom_function_register(&strptime_function);
|
|
|
|
res |= ast_custom_function_register(&eval_function);
|
2006-02-15 00:59:01 +00:00
|
|
|
res |= ast_custom_function_register(&keypadhash_function);
|
2006-05-18 00:00:32 +00:00
|
|
|
res |= ast_custom_function_register(&sprintf_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");
|