2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005-2006, Digium, Inc.
5 * Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
6 * Portions Copyright (C) 2005, Anthony Minessale II
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief String manipulation dialplan functions
23 * \author Tilghman Lesher
24 * \author Anothony Minessale II
30 #include <sys/types.h>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/localtime.h"
45 static int function_fieldqty(struct ast_channel *chan, char *cmd,
46 char *parse, char *buf, size_t len)
50 AST_DECLARE_APP_ARGS(args,
55 AST_STANDARD_APP_ARGS(args, parse);
57 pbx_retrieve_variable(chan, args.varname, &varval, buf, len, NULL);
58 while (strsep(&varval, args.delim))
63 snprintf(buf, len, "%d", fieldcount);
68 static struct ast_custom_function fieldqty_function = {
70 .synopsis = "Count the fields, with an arbitrary delimiter",
71 .syntax = "FIELDQTY(<varname>,<delim>)",
72 .read = function_fieldqty,
75 static int filter(struct ast_channel *chan, char *cmd, char *parse, char *buf,
78 AST_DECLARE_APP_ARGS(args,
84 AST_STANDARD_APP_ARGS(args, parse);
87 ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
91 for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
92 if (strchr(args.allowed, *(args.string)))
93 *outbuf++ = *(args.string);
100 static struct ast_custom_function filter_function = {
102 .synopsis = "Filter the string to include only the allowed characters",
103 .syntax = "FILTER(<allowed-chars>,<string>)",
107 static int regex(struct ast_channel *chan, char *cmd, char *parse, char *buf,
110 AST_DECLARE_APP_ARGS(args,
120 AST_NONSTANDARD_APP_ARGS(args, parse, '"');
122 ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
124 if ((errcode = regcomp(®exbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
125 regerror(errcode, ®exbuf, buf, len);
126 ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
129 if (!regexec(®exbuf, args.str, 0, NULL, 0))
137 static struct ast_custom_function regex_function = {
140 "Regular Expression: Returns 1 if data matches regular expression.",
141 .syntax = "REGEX(\"<regular expression>\" <data>)",
145 static int array(struct ast_channel *chan, char *cmd, char *var,
148 AST_DECLARE_APP_ARGS(arg1,
149 AST_APP_ARG(var)[100];
151 AST_DECLARE_APP_ARGS(arg2,
152 AST_APP_ARG(val)[100];
157 value2 = ast_strdupa(value);
161 /* The functions this will generally be used with are SORT and ODBC_*, which
162 * both return comma-delimited lists. However, if somebody uses literal lists,
163 * their commas will be translated to vertical bars by the load, and I don't
164 * want them to be surprised by the result. Hence, we prefer commas as the
165 * delimiter, but we'll fall back to vertical bars if commas aren't found.
167 ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
168 if (strchr(var, ','))
169 AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
171 AST_STANDARD_APP_ARGS(arg1, var);
173 if (strchr(value2, ','))
174 AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
176 AST_STANDARD_APP_ARGS(arg2, value2);
178 for (i = 0; i < arg1.argc; i++) {
179 ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
182 pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
184 /* We could unset the variable, by passing a NULL, but due to
185 * pushvar semantics, that could create some undesired behavior. */
186 pbx_builtin_setvar_helper(chan, arg1.var[i], "");
193 static struct ast_custom_function array_function = {
195 .synopsis = "Allows setting multiple variables at once",
196 .syntax = "ARRAY(var1[,var2[...][,varN]])",
199 "The comma-separated list passed as a value to which the function is set will\n"
200 "be interpreted as a set of values to which the comma-separated list of\n"
201 "variable names in the argument should be set.\n"
202 "Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2\n"
203 "Note: remember to either backslash your commas in extensions.conf or quote the\n"
204 "entire argument, since Set can take multiple arguments itself.\n",
207 static int quote(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
209 char *bufptr = buf, *dataptr = data;
211 for (; bufptr < buf + len - 1; dataptr++) {
212 if (*dataptr == '\\') {
215 } else if (*dataptr == '"') {
218 } else if (*dataptr == '\0') {
221 *bufptr++ = *dataptr;
229 static struct ast_custom_function quote_function = {
231 .synopsis = "Quotes a given string, escaping embedded quotes as necessary",
232 .syntax = "QUOTE(<string>)",
237 static int len(struct ast_channel *chan, char *cmd, char *data, char *buf,
243 length = strlen(data);
245 snprintf(buf, len, "%d", length);
250 static struct ast_custom_function len_function = {
252 .synopsis = "Returns the length of the argument given",
253 .syntax = "LEN(<string>)",
257 static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
258 char *buf, size_t len)
260 AST_DECLARE_APP_ARGS(args,
262 AST_APP_ARG(timezone);
270 if (ast_strlen_zero(parse)) {
272 "Asterisk function STRFTIME() requires an argument.\n");
276 AST_STANDARD_APP_ARGS(args, parse);
278 ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
279 ast_localtime(&epochi, &tm, args.timezone);
284 if (!strftime(buf, len, args.format, &tm))
285 ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
292 static struct ast_custom_function strftime_function = {
294 .synopsis = "Returns the current date/time in a specified format.",
295 .syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
296 .read = acf_strftime,
299 static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
300 char *buf, size_t len)
302 AST_DECLARE_APP_ARGS(args,
303 AST_APP_ARG(timestring);
304 AST_APP_ARG(timezone);
309 memset(&time, 0, sizeof(struct tm));
315 "Asterisk function STRPTIME() requires an argument.\n");
319 AST_STANDARD_APP_ARGS(args, data);
321 if (ast_strlen_zero(args.format)) {
323 "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
327 if (!strptime(args.timestring, args.format, &time)) {
328 ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
330 snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
336 static struct ast_custom_function strptime_function = {
339 "Returns the epoch of the arbitrary date/time string structured as described in the format.",
340 .syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
342 "This is useful for converting a date into an EPOCH time, possibly to pass to\n"
343 "an application like SayUnixTime or to calculate the difference between two\n"
347 " ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
348 .read = acf_strptime,
351 static int function_eval(struct ast_channel *chan, char *cmd, char *data,
352 char *buf, size_t len)
356 if (ast_strlen_zero(data)) {
357 ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
361 pbx_substitute_variables_helper(chan, data, buf, len - 1);
366 static struct ast_custom_function eval_function = {
368 .synopsis = "Evaluate stored variables.",
369 .syntax = "EVAL(<variable>)",
370 .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
371 "When a variable or expression is in the dialplan, it will be\n"
372 "evaluated at runtime. However, if the result of the evaluation\n"
373 "is in fact a variable or expression, using EVAL will have it\n"
374 "evaluated a second time. For example, if the variable ${MYVAR}\n"
375 "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
376 "in the dialplan will be the contents of the variable, OTHERVAR.\n"
377 "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
378 "left with \"${OTHERVAR}\".\n",
379 .read = function_eval,
382 static int keypadhash(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
384 char *bufptr, *dataptr;
386 for (bufptr = buf, dataptr = data; bufptr < buf + len - 1; dataptr++) {
387 if (*dataptr == '1') {
389 } else if (strchr("AaBbCc2", *dataptr)) {
391 } else if (strchr("DdEeFf3", *dataptr)) {
393 } else if (strchr("GgHhIi4", *dataptr)) {
395 } else if (strchr("JjKkLl5", *dataptr)) {
397 } else if (strchr("MmNnOo6", *dataptr)) {
399 } else if (strchr("PpQqRrSs7", *dataptr)) {
401 } else if (strchr("TtUuVv8", *dataptr)) {
403 } else if (strchr("WwXxYyZz9", *dataptr)) {
405 } else if (*dataptr == '0') {
407 } else if (*dataptr == '\0') {
417 static struct ast_custom_function keypadhash_function = {
418 .name = "KEYPADHASH",
419 .synopsis = "Hash the letters in the string into the equivalent keypad numbers.",
420 .syntax = "KEYPADHASH(<string>)",
422 .desc = "Example: ${KEYPADHASH(Les)} returns \"537\"\n",
425 static char *tdesc = "String handling dialplan functions";
427 int unload_module(void)
431 res |= ast_custom_function_unregister(&fieldqty_function);
432 res |= ast_custom_function_unregister(&filter_function);
433 res |= ast_custom_function_unregister(®ex_function);
434 res |= ast_custom_function_unregister(&array_function);
435 res |= ast_custom_function_unregister("e_function);
436 res |= ast_custom_function_unregister(&len_function);
437 res |= ast_custom_function_unregister(&strftime_function);
438 res |= ast_custom_function_unregister(&strptime_function);
439 res |= ast_custom_function_unregister(&eval_function);
440 res |= ast_custom_function_unregister(&keypadhash_function);
445 int load_module(void)
449 res |= ast_custom_function_register(&fieldqty_function);
450 res |= ast_custom_function_register(&filter_function);
451 res |= ast_custom_function_register(®ex_function);
452 res |= ast_custom_function_register(&array_function);
453 res |= ast_custom_function_register("e_function);
454 res |= ast_custom_function_register(&len_function);
455 res |= ast_custom_function_register(&strftime_function);
456 res |= ast_custom_function_register(&strptime_function);
457 res |= ast_custom_function_register(&eval_function);
458 res |= ast_custom_function_register(&keypadhash_function);
463 const char *description(void)
475 return ASTERISK_GPL_KEY;