Newer
Older
.write = hash_write,
.read = hash_read,
};
static struct ast_custom_function hashkeys_function = {
.name = "HASHKEYS",
.read = hashkeys_read,
};
static struct ast_custom_function array_function = {
.write = array,
static int quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *bufptr = buf, *dataptr = data;
if (len < 3){ /* at least two for quotes and one for binary zero */
ast_log(LOG_ERROR, "Not enough buffer");
return -1;
}
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "No argument specified!\n");
ast_copy_string(buf, "\"\"", len);
return 0;
}
for (; bufptr < buf + len - 3; 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",
static int csv_quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *bufptr = buf, *dataptr = data;
if (len < 3) { /* at least two for quotes and one for binary zero */
ast_log(LOG_ERROR, "Not enough buffer");
return -1;
}
if (ast_strlen_zero(data)) {
ast_copy_string(buf, "\"\"", len);
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
return 0;
}
*bufptr++ = '"';
for (; bufptr < buf + len - 3; dataptr++){
if (*dataptr == '"') {
*bufptr++ = '"';
*bufptr++ = '"';
} else if (*dataptr == '\0') {
break;
} else {
*bufptr++ = *dataptr;
}
}
*bufptr++ = '"';
*bufptr='\0';
return 0;
}
static struct ast_custom_function csv_quote_function = {
.name = "CSV_QUOTE",
.read = csv_quote,
};
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
int length = 0;
length = strlen(data);
snprintf(buf, buflen, "%d", length);
static struct ast_custom_function len_function = {
.read = len,
Kevin P. Fleming
committed
static int acf_strftime(struct ast_channel *chan, const char *cmd, char *parse,
char *buf, size_t buflen)
Kevin P. Fleming
committed
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(epoch);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
struct timeval when;
Tilghman Lesher
committed
struct ast_tm tm;
Kevin P. Fleming
committed
buf[0] = '\0';
AST_STANDARD_APP_ARGS(args, parse);
ast_get_timeval(args.epoch, &when, ast_tvnow(), NULL);
ast_localtime(&when, &tm, args.timezone);
if (!args.format)
args.format = "%c";
if (ast_strftime(buf, buflen, args.format, &tm) <= 0)
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
buf[buflen - 1] = '\0';
Kevin P. Fleming
committed
}
static struct ast_custom_function strftime_function = {
Kevin P. Fleming
committed
.name = "STRFTIME",
.read = acf_strftime,
};
static int acf_strptime(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t buflen)
AST_APP_ARG(timestring);
AST_APP_ARG(timezone);
AST_APP_ARG(format);
struct ast_tm tm;
buf[0] = '\0';
if (!data) {
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>)");
if (!ast_strptime(args.timestring, args.format, &tm)) {
ast_log(LOG_WARNING, "STRPTIME() found no time specified within the string\n");
struct timeval when;
when = ast_mktime(&tm, args.timezone);
snprintf(buf, buflen, "%d", (int) when.tv_sec);
static struct ast_custom_function strptime_function = {
.name = "STRPTIME",
.read = acf_strptime,
};
static int function_eval(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t buflen)
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
pbx_substitute_variables_helper(chan, data, buf, buflen - 1);
static int function_eval2(struct ast_channel *chan, const char *cmd, char *data,
struct ast_str **buf, ssize_t buflen)
{
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
return -1;
}
ast_str_substitute_variables(buf, buflen, chan, data);
return 0;
}
static struct ast_custom_function eval_function = {
.name = "EVAL",
.read = function_eval,
static int keypadhash(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr, *dataptr;
for (bufptr = buf, dataptr = data; bufptr < buf + buflen - 1; dataptr++) {
if (*dataptr == '\0') {
*bufptr++ = '\0';
break;
} else if (*dataptr == '1') {
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
*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';
}
}
buf[buflen - 1] = '\0';
return 0;
}
static struct ast_custom_function keypadhash_function = {
.name = "KEYPADHASH",
.read = keypadhash,
};
static int string_toupper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr = buf, *dataptr = data;
while ((bufptr < buf + buflen - 1) && (*bufptr++ = toupper(*dataptr++)));
return 0;
}
static int string_toupper2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
{
char *bufptr, *dataptr = data;
if (buflen > -1) {
ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
}
bufptr = ast_str_buffer(*buf);
while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = toupper(*dataptr++)));
ast_str_update(*buf);
return 0;
}
static struct ast_custom_function toupper_function = {
.name = "TOUPPER",
.read = string_toupper,
};
static int string_tolower(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr = buf, *dataptr = data;
while ((bufptr < buf + buflen - 1) && (*bufptr++ = tolower(*dataptr++)));
return 0;
}
static int string_tolower2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
{
char *bufptr, *dataptr = data;
if (buflen > -1) {
ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
}
bufptr = ast_str_buffer(*buf);
while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = tolower(*dataptr++)));
ast_str_update(*buf);
return 0;
}
static struct ast_custom_function tolower_function = {
.name = "TOLOWER",
.read = string_tolower,
};
Tilghman Lesher
committed
static int shift_pop(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Tilghman Lesher
committed
#define beginning (cmd[0] == 'S') /* SHIFT */
char *after, delimiter[2] = ",", *varsubst;
size_t unused;
struct ast_str *before = ast_str_thread_get(&result_buf, 16);
char *(*search_func)(const char *s, int c) = (beginning ? strchr : strrchr);
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(delimiter);
);
Tilghman Lesher
committed
if (!before) {
return -1;
}
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(args, data);
if (ast_strlen_zero(args.var)) {
Tilghman Lesher
committed
ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);
return -1;
}
Tilghman Lesher
committed
varsubst = alloca(strlen(args.var) + 4);
sprintf(varsubst, "${%s}", args.var);
ast_str_substitute_variables(&before, 0, chan, varsubst);
Tilghman Lesher
committed
if (args.argc > 1 && !ast_strlen_zero(args.delimiter)) {
ast_get_encoded_char(args.delimiter, delimiter, &unused);
Tilghman Lesher
committed
if (!ast_str_strlen(before)) {
/* Nothing to pop */
return -1;
}
Tilghman Lesher
committed
if (!(after = search_func(ast_str_buffer(before), delimiter[0]))) {
/* Only one entry in array */
ast_str_set(buf, len, "%s", ast_str_buffer(before));
pbx_builtin_setvar_helper(chan, args.var, "");
} else {
*after++ = '\0';
Tilghman Lesher
committed
ast_str_set(buf, len, "%s", beginning ? ast_str_buffer(before) : after);
pbx_builtin_setvar_helper(chan, args.var, beginning ? after : ast_str_buffer(before));
}
return 0;
Tilghman Lesher
committed
#undef beginning
}
static struct ast_custom_function shift_function = {
.name = "SHIFT",
Tilghman Lesher
committed
.read2 = shift_pop,
};
static struct ast_custom_function pop_function = {
.name = "POP",
Tilghman Lesher
committed
.read2 = shift_pop,
Tilghman Lesher
committed
static int unshift_push(struct ast_channel *chan, const char *cmd, char *data, const char *new_value)
Tilghman Lesher
committed
#define beginning (cmd[0] == 'U') /* UNSHIFT */
char delimiter[2] = ",", *varsubst;
size_t unused;
struct ast_str *buf, *previous_value;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(delimiter);
);
Tilghman Lesher
committed
if (!(buf = ast_str_thread_get(&result_buf, 16)) ||
!(previous_value = ast_str_thread_get(&tmp_buf, 16))) {
return -1;
}
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(args, data);
Tilghman Lesher
committed
if (ast_strlen_zero(args.var)) {
ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);
return -1;
}
Tilghman Lesher
committed
if (args.argc > 1 && !ast_strlen_zero(args.delimiter)) {
ast_get_encoded_char(args.delimiter, delimiter, &unused);
Tilghman Lesher
committed
varsubst = alloca(strlen(args.var) + 4);
sprintf(varsubst, "${%s}", args.var);
ast_str_substitute_variables(&previous_value, 0, chan, varsubst);
Tilghman Lesher
committed
if (!ast_str_strlen(previous_value)) {
ast_str_set(&buf, 0, "%s", new_value);
Tilghman Lesher
committed
ast_str_set(&buf, 0, "%s%c%s",
beginning ? new_value : ast_str_buffer(previous_value),
delimiter[0],
beginning ? ast_str_buffer(previous_value) : new_value);
}
pbx_builtin_setvar_helper(chan, args.var, ast_str_buffer(buf));
return 0;
Tilghman Lesher
committed
#undef beginning
}
static struct ast_custom_function push_function = {
.name = "PUSH",
Tilghman Lesher
committed
.write = unshift_push,
Tilghman Lesher
committed
static struct ast_custom_function unshift_function = {
.name = "UNSHIFT",
.write = unshift_push,
};
static int passthru(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Tilghman Lesher
committed
ast_str_set(buf, len, "%s", data);
return 0;
Tilghman Lesher
committed
static struct ast_custom_function passthru_function = {
.name = "PASSTHRU",
.read2 = passthru,
#ifdef TEST_FRAMEWORK
AST_TEST_DEFINE(test_FILTER)
{
int i, res = AST_TEST_PASS;
const char *test_strings[][2] = {
{"A-R", "DAHDI"},
{"A\\-R", "A"},
{"\\x41-R", "DAHDI"},
{"0-9A-Ca-c", "0042133333A12212"},
{"0-9a-cA-C_+\\-", "0042133333A12212"},
{NULL, NULL},
};
switch (cmd) {
case TEST_INIT:
info->name = "func_FILTER_test";
Tilghman Lesher
committed
info->category = "/funcs/func_strings/";
info->summary = "Test FILTER function";
info->description = "Verify FILTER behavior";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
for (i = 0; test_strings[i][0]; i++) {
char tmp[256], tmp2[256] = "";
snprintf(tmp, sizeof(tmp), "${FILTER(%s,0042133333&DAHDI/g1/2212)}", test_strings[i][0]);
pbx_substitute_variables_helper(NULL, tmp, tmp2, sizeof(tmp2) - 1);
if (strcmp(test_strings[i][1], tmp2)) {
ast_test_status_update(test, "Format string '%s' substituted to '%s'. Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][1]);
res = AST_TEST_FAIL;
}
}
return res;
}
#endif
static int unload_module(void)
{
int res = 0;
AST_TEST_UNREGISTER(test_FILTER);
res |= ast_custom_function_unregister(&fieldqty_function);
res |= ast_custom_function_unregister(&filter_function);
Tilghman Lesher
committed
res |= ast_custom_function_unregister(&replace_function);
Tilghman Lesher
committed
res |= ast_custom_function_unregister(&listfilter_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(&csv_quote_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);
res |= ast_custom_function_unregister(&hashkeys_function);
res |= ast_custom_function_unregister(&hash_function);
res |= ast_unregister_application(app_clearhash);
res |= ast_custom_function_unregister(&toupper_function);
res |= ast_custom_function_unregister(&tolower_function);
res |= ast_custom_function_unregister(&shift_function);
res |= ast_custom_function_unregister(&pop_function);
res |= ast_custom_function_unregister(&push_function);
res |= ast_custom_function_unregister(&unshift_function);
Tilghman Lesher
committed
res |= ast_custom_function_unregister(&passthru_function);
return res;
}
static int load_module(void)
{
int res = 0;
AST_TEST_REGISTER(test_FILTER);
res |= ast_custom_function_register(&fieldqty_function);
res |= ast_custom_function_register(&filter_function);
Tilghman Lesher
committed
res |= ast_custom_function_register(&replace_function);
Tilghman Lesher
committed
res |= ast_custom_function_register(&listfilter_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(&csv_quote_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);
res |= ast_custom_function_register(&hashkeys_function);
res |= ast_custom_function_register(&hash_function);
res |= ast_register_application_xml(app_clearhash, exec_clearhash);
res |= ast_custom_function_register(&toupper_function);
res |= ast_custom_function_register(&tolower_function);
res |= ast_custom_function_register(&shift_function);
res |= ast_custom_function_register(&pop_function);
res |= ast_custom_function_register(&push_function);
res |= ast_custom_function_register(&unshift_function);
Tilghman Lesher
committed
res |= ast_custom_function_register(&passthru_function);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");