Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 2005-2006, Digium, Inc.
* Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
* Portions Copyright (C) 2005, Anthony Minessale II
* 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.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
*
* \brief String manipulation dialplan functions
*
* \author Tilghman Lesher
* \author Anothony Minessale II
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Kevin P. Fleming
committed
#include "asterisk/localtime.h"
#include "asterisk/options.h"
static int function_fieldqty(struct ast_channel *chan, const char *cmd,
char *varsubst, varval[8192] = "", *varval2 = varval;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(delim);
);
AST_STANDARD_APP_ARGS(args, parse);
if (args.delim) {
Tilghman Lesher
committed
if (args.delim[0] == '\\') {
if (args.delim[1] == 'n')
ast_copy_string(args.delim, "\n", 2);
else if (args.delim[1] == 't')
ast_copy_string(args.delim, "\t", 2);
else if (args.delim[1])
ast_copy_string(args.delim, &args.delim[1], 2);
else
ast_copy_string(args.delim, "-", 2);
}
varsubst = alloca(strlen(args.varname) + 4);
sprintf(varsubst, "${%s}", args.varname);
pbx_substitute_variables_helper(chan, varsubst, varval, sizeof(varval) - 1);
if (ast_strlen_zero(varval2))
fieldcount = 0;
else {
while (strsep(&varval2, args.delim))
fieldcount++;
}
snprintf(buf, len, "%d", fieldcount);
static struct ast_custom_function fieldqty_function = {
.name = "FIELDQTY",
.synopsis = "Count the fields, with an arbitrary delimiter",
Tilghman Lesher
committed
.syntax = "FIELDQTY(<varname>,<delim>)",
.read = function_fieldqty,
};
Tilghman Lesher
committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
static int get_filter_char(const char *stream, char *result, size_t *consumed)
{
int i;
*consumed = 1;
*result = 0;
if (*stream == '\\') {
*consumed = 2;
switch (*(stream + 1)) {
case 'n':
*result = '\n';
break;
case 'r':
*result = '\r';
break;
case 't':
*result = '\t';
break;
case 'x':
/* Hexadecimal */
if (strchr("0123456789ABCDEFabcdef", *(stream + 2)) && *(stream + 2) != '\0') {
*consumed = 3;
if (*(stream + 2) <= '9')
*result = *(stream + 2) - '0';
else if (*(stream + 2) <= 'F')
*result = *(stream + 2) - 'A' + 10;
else
*result = *(stream + 2) - 'a' + 10;
} else {
ast_log(LOG_ERROR, "Illegal character '%c' in hexadecimal string\n", *(stream + 2));
return -1;
}
if (strchr("0123456789ABCDEFabcdef", *(stream + 3)) && *(stream + 3) != '\0') {
*consumed = 4;
*result <<= 4;
if (*(stream + 3) <= '9')
*result += *(stream + 3) - '0';
else if (*(stream + 3) <= 'F')
*result += *(stream + 3) - 'A' + 10;
else
*result += *(stream + 3) - 'a' + 10;
}
break;
case '0':
/* Octal */
*consumed = 2;
for (i = 2; ; i++) {
if (strchr("01234567", *(stream + i)) && *(stream + i) != '\0') {
(*consumed)++;
ast_debug(5, "result was %d, ", *result);
*result <<= 3;
*result += *(stream + i) - '0';
ast_debug(5, "is now %d\n", *result);
} else
break;
}
break;
default:
*result = *(stream + 1);
}
} else {
*result = *stream;
*consumed = 1;
}
return 0;
}
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(allowed);
AST_APP_ARG(string);
Tilghman Lesher
committed
char *outbuf = buf, ac;
char allowed[256] = "";
size_t allowedlen = 0;
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(args, parse);
Tilghman Lesher
committed
ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
}
Tilghman Lesher
committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Expand ranges */
for (; *(args.allowed) && allowedlen < sizeof(allowed); (args.allowed)++) {
char c1 = 0, c2 = 0;
size_t consumed = 0;
if (get_filter_char(args.allowed, &c1, &consumed))
return -1;
args.allowed += consumed;
if (*(args.allowed) == '-') {
if (get_filter_char(args.allowed + 1, &c2, &consumed))
c2 = -1;
args.allowed += consumed + 1;
/*!\note
* Looks a little strange, until you realize that we can overflow
* the size of a char.
*/
for (ac = c1; ac != c2 && allowedlen < sizeof(allowed) - 1; ac++)
allowed[allowedlen++] = ac;
allowed[allowedlen++] = ac;
ast_debug(4, "c1=%d, c2=%d\n", c1, c2);
/* Decrement before the loop increment */
(args.allowed)--;
} else
allowed[allowedlen++] = c1;
}
ast_debug(1, "Allowed: %s\n", allowed);
for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
Tilghman Lesher
committed
if (strchr(allowed, *(args.string)))
}
*outbuf = '\0';
}
static struct ast_custom_function filter_function = {
.name = "FILTER",
.synopsis = "Filter the string to include only the allowed characters",
Tilghman Lesher
committed
.syntax = "FILTER(<allowed-chars>,<string>)",
.read = filter,
.desc =
"Permits all characters listed in <allowed-chars>, filtering all others out.\n"
"In addition to literally listing the characters, you may also use ranges of\n"
"characters (delimited by a '-'), as well as hexadecimal characters started\n"
"with a \\x (i.e. \\x20) and octal characters started with \\0 (i.e. \\040).\n"
"Also, \\t, \\n, and \\r are recognized. If you want a literal '-' character,\n"
"simply prefix it with a '\\'\n",
};
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(null);
AST_APP_ARG(reg);
AST_APP_ARG(str);
int errcode;
regex_t regexbuf;
AST_NONSTANDARD_APP_ARGS(args, parse, '"');
Tilghman Lesher
committed
if (args.argc != 3) {
ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
return -1;
}
if ((*args.str == ' ') || (*args.str == '\t'))
args.str++;
Tilghman Lesher
committed
ast_debug(1, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
if ((errcode = regcomp(®exbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
regerror(errcode, ®exbuf, buf, len);
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
return -1;
Russell Bryant
committed
}
Russell Bryant
committed
strcpy(buf, regexec(®exbuf, args.str, 0, NULL, 0) ? "0" : "1");
Russell Bryant
committed
regfree(®exbuf);
static struct ast_custom_function regex_function = {
Russell Bryant
committed
.synopsis = "Regular Expression",
Steve Murphy
committed
.desc =
"Returns 1 if data matches regular expression, or 0 otherwise.\n"
"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>)",
.read = regex,
#define HASH_PREFIX "~HASH~%s~"
#define HASH_FORMAT HASH_PREFIX "%s~"
static char *app_clearhash = "ClearHash";
static char *syn_clearhash = "Clear the keys from a specified hashname";
static char *desc_clearhash =
"ClearHash(<hashname>)\n"
" Clears all keys out of the specified hashname\n";
/* This function probably should migrate to main/pbx.c, as pbx_builtin_clearvar_prefix() */
static void clearvar_prefix(struct ast_channel *chan, const char *prefix)
{
struct ast_var_t *var;
int len = strlen(prefix);
AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->varshead, var, entries) {
if (strncasecmp(prefix, ast_var_name(var), len) == 0) {
AST_LIST_REMOVE_CURRENT(&chan->varshead, entries);
Tilghman Lesher
committed
ast_free(var);
}
}
AST_LIST_TRAVERSE_SAFE_END
}
static int exec_clearhash(struct ast_channel *chan, void *data)
{
char prefix[80];
snprintf(prefix, sizeof(prefix), HASH_PREFIX, data ? (char *)data : "null");
clearvar_prefix(chan, prefix);
return 0;
}
static int array(struct ast_channel *chan, const char *cmd, char *var,
AST_DECLARE_APP_ARGS(arg1,
);
AST_DECLARE_APP_ARGS(arg2,
char *origvar = "", *value2, varname[256];
int i, ishash = 0;
if (!strcmp(cmd, "HASH")) {
const char *var2 = pbx_builtin_getvar_helper(chan, "~ODBCFIELDS~");
origvar = var;
if (var2)
var = ast_strdupa(var2);
else
return -1;
ishash = 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,
* 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.
*/
ast_debug(1, "array (%s=%s)\n", var, value2);
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(arg1, var);
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(arg2, value2);
for (i = 0; i < arg1.argc; i++) {
ast_debug(1, "array set value (%s=%s)\n", arg1.var[i],
arg2.val[i]);
if (i < arg2.argc) {
if (ishash) {
snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
pbx_builtin_setvar_helper(chan, varname, arg2.val[i]);
} else {
pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
}
} else {
/* We could unset the variable, by passing a NULL, but due to
* pushvar semantics, that could create some undesired behavior. */
if (ishash) {
snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
pbx_builtin_setvar_helper(chan, varname, "");
} else {
pbx_builtin_setvar_helper(chan, arg1.var[i], "");
}
static int hashkeys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct ast_var_t *newvar;
int plen;
char prefix[80];
snprintf(prefix, sizeof(prefix), HASH_PREFIX, data);
plen = strlen(prefix);
memset(buf, 0, len);
AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
if (strncasecmp(prefix, ast_var_name(newvar), plen) == 0) {
/* Copy everything after the prefix */
strncat(buf, ast_var_name(newvar) + plen, len);
/* Trim the trailing ~ */
buf[strlen(buf) - 1] = ',';
}
}
/* Trim the trailing comma */
buf[strlen(buf) - 1] = '\0';
return 0;
}
static int hash_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
{
char varname[256];
AST_DECLARE_APP_ARGS(arg,
AST_APP_ARG(hashname);
AST_APP_ARG(hashkey);
);
Tilghman Lesher
committed
if (!strchr(var, ',')) {
/* Single argument version */
return array(chan, "HASH", var, value);
}
AST_STANDARD_APP_ARGS(arg, var);
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
pbx_builtin_setvar_helper(chan, varname, value);
return 0;
}
static int hash_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
{
char varname[256];
const char *varvalue;
AST_DECLARE_APP_ARGS(arg,
AST_APP_ARG(hashname);
AST_APP_ARG(hashkey);
);
AST_STANDARD_APP_ARGS(arg, data);
if (arg.argc == 2) {
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
varvalue = pbx_builtin_getvar_helper(chan, varname);
if (varvalue)
ast_copy_string(buf, varvalue, len);
else
*buf = '\0';
} else if (arg.argc == 1) {
char colnames[4096];
int i;
AST_DECLARE_APP_ARGS(arg2,
AST_APP_ARG(col)[100];
);
/* Get column names, in no particular order */
hashkeys_read(chan, "HASHKEYS", arg.hashname, colnames, sizeof(colnames));
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
AST_NONSTANDARD_APP_ARGS(arg2, colnames, ',');
*buf = '\0';
/* Now get the corresponding column values, in exactly the same order */
for (i = 0; i < arg2.argc; i++) {
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg2.col[i]);
varvalue = pbx_builtin_getvar_helper(chan, varname);
strncat(buf, varvalue, len);
strncat(buf, ",", len);
}
/* Strip trailing comma */
buf[strlen(buf) - 1] = '\0';
}
return 0;
}
static struct ast_custom_function hash_function = {
.name = "HASH",
.synopsis = "Implementation of a dialplan associative array",
Tilghman Lesher
committed
.syntax = "HASH(hashname[,hashkey])",
.write = hash_write,
.read = hash_read,
.desc =
"In two argument mode, gets and sets values to corresponding keys within a named\n"
"associative array. The single-argument mode will only work when assigned to from\n"
"a function defined by func_odbc.so.\n",
};
static struct ast_custom_function hashkeys_function = {
.name = "HASHKEYS",
.synopsis = "Retrieve the keys of a HASH()",
.syntax = "HASHKEYS(<hashname>)",
.read = hashkeys_read,
.desc =
"Returns a comma-delimited list of the current keys of an associative array\n"
"defined by the HASH() function. Note that if you iterate over the keys of\n"
"the result, adding keys during iteration will cause the result of the HASHKEYS\n"
"function to change.\n",
};
static struct ast_custom_function array_function = {
.name = "ARRAY",
.synopsis = "Allows setting multiple variables at once",
Tilghman Lesher
committed
.syntax = "ARRAY(var1[,var2[...][,varN]])",
.write = array,
"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"
Tilghman Lesher
committed
"Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2.\n",
static int acf_sprintf(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Joshua Colp
committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
{
#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++], "%d", &tmpi) != 1) {
Joshua Colp
committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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",
Tilghman Lesher
committed
.syntax = "SPRINTF(<format>,<arg1>[,...<argN>])",
Joshua Colp
committed
.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",
};
static int quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
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';
}
static struct ast_custom_function quote_function = {
.name = "QUOTE",
.synopsis = "Quotes a given string, escaping embedded quotes as necessary",
.syntax = "QUOTE(<string>)",
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf,
{
int length = 0;
length = strlen(data);
snprintf(buf, len, "%d", length);
static struct ast_custom_function len_function = {
.name = "LEN",
.synopsis = "Returns the length of the argument given",
.syntax = "LEN(<string>)",
.read = len,
Kevin P. Fleming
committed
static int acf_strftime(struct ast_channel *chan, const char *cmd, char *parse,
Kevin P. Fleming
committed
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(epoch);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
Tilghman Lesher
committed
struct timeval tv;
struct ast_tm tm;
Kevin P. Fleming
committed
buf[0] = '\0';
AST_STANDARD_APP_ARGS(args, parse);
Tilghman Lesher
committed
ast_get_timeval(args.epoch, &tv, ast_tvnow(), NULL);
ast_localtime(&tv, &tm, args.timezone);
if (!args.format)
args.format = "%c";
Tilghman Lesher
committed
if (ast_strftime(buf, len, args.format, &tm) <= 0)
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
buf[len - 1] = '\0';
Kevin P. Fleming
committed
}
static struct ast_custom_function strftime_function = {
Kevin P. Fleming
committed
.name = "STRFTIME",
.synopsis = "Returns the current date/time in a specified format.",
Tilghman Lesher
committed
.syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
Tilghman Lesher
committed
.desc =
"STRFTIME sports all of the same formats as the underlying C function\n"
"strftime(3) - see the man page for details. It also supports the\n"
"following format:\n"
" %[n]q - fractions of a second, with leading zeroes. For example, %3q will\n"
" give milliseconds and %1q will give tenths of a second. The default\n"
" is to output milliseconds (n=3). The common case is to use it in\n"
" combination with %S, as in \"%S.%3q\".\n",
Kevin P. Fleming
committed
.read = acf_strftime,
};
static int acf_strptime(struct ast_channel *chan, const char *cmd, char *data,
AST_APP_ARG(timestring);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
Tilghman Lesher
committed
union {
struct ast_tm atm;
struct tm time;
} t = { { 0, }, };
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,
Tilghman Lesher
committed
"No format supplied to STRPTIME(<timestring>,<timezone>,<format>)");
Tilghman Lesher
committed
if (!strptime(args.timestring, args.format, &t.time)) {
ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
} else {
struct timeval tv = ast_mktime(&t.atm, args.timezone);
snprintf(buf, len, "%d", (int) tv.tv_sec);
static struct ast_custom_function strptime_function = {
.synopsis =
"Returns the epoch of the arbitrary date/time string structured as described in the format.",
Tilghman Lesher
committed
.syntax = "STRPTIME(<datetime>,<timezone>,<format>)",
"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"
Tilghman Lesher
committed
" ${STRPTIME(2006-03-01 07:30:35,America/Chicago,%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
static int function_eval(struct ast_channel *chan, const char *cmd, char *data,
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
pbx_substitute_variables_helper(chan, data, buf, len - 1);
static struct ast_custom_function eval_function = {
.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"
.read = function_eval,
};
static int keypadhash(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
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
856
857
858
859
860
861
862
863
864
865
{
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",
};
static int unload_module(void)
{
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);
res |= ast_custom_function_unregister("e_function);
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);
res |= ast_custom_function_unregister(&keypadhash_function);
Joshua Colp
committed
res |= ast_custom_function_unregister(&sprintf_function);
res |= ast_custom_function_unregister(&hashkeys_function);
res |= ast_custom_function_unregister(&hash_function);
res |= ast_unregister_application(app_clearhash);
return res;
}
static int load_module(void)
{
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);
res |= ast_custom_function_register("e_function);
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);
res |= ast_custom_function_register(&keypadhash_function);
Joshua Colp
committed
res |= ast_custom_function_register(&sprintf_function);
res |= ast_custom_function_register(&hashkeys_function);
res |= ast_custom_function_register(&hash_function);
res |= ast_register_application(app_clearhash, exec_clearhash, syn_clearhash, desc_clearhash);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");