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 len(struct ast_channel *chan, char *cmd, char *data, char *buf,
213 length = strlen(data);
215 snprintf(buf, len, "%d", length);
220 static struct ast_custom_function len_function = {
222 .synopsis = "Returns the length of the argument given",
223 .syntax = "LEN(<string>)",
227 static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
228 char *buf, size_t len)
230 AST_DECLARE_APP_ARGS(args,
232 AST_APP_ARG(timezone);
240 if (ast_strlen_zero(parse)) {
242 "Asterisk function STRFTIME() requires an argument.\n");
246 AST_STANDARD_APP_ARGS(args, parse);
248 if (ast_strlen_zero(args.epoch) || !sscanf(args.epoch, "%ld", &epochi)) {
249 struct timeval tv = ast_tvnow();
253 ast_localtime(&epochi, &time, args.timezone);
255 if (!strftime(buf, len, args.format ? args.format : "%c", &time))
256 ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
263 static struct ast_custom_function strftime_function = {
265 .synopsis = "Returns the current date/time in a specified format.",
266 .syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
267 .read = acf_strftime,
270 static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
271 char *buf, size_t len)
273 AST_DECLARE_APP_ARGS(args,
274 AST_APP_ARG(timestring);
275 AST_APP_ARG(timezone);
280 memset(&time, 0, sizeof(struct tm));
286 "Asterisk function STRPTIME() requires an argument.\n");
290 AST_STANDARD_APP_ARGS(args, data);
292 if (ast_strlen_zero(args.format)) {
294 "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
298 if (!strptime(args.timestring, args.format, &time)) {
299 ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
301 snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
307 static struct ast_custom_function strptime_function = {
310 "Returns the epoch of the arbitrary date/time string structured as described in the format.",
311 .syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
313 "This is useful for converting a date into an EPOCH time, possibly to pass to\n"
314 "an application like SayUnixTime or to calculate the difference between two\n"
318 " ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
319 .read = acf_strptime,
322 static int function_eval(struct ast_channel *chan, char *cmd, char *data,
323 char *buf, size_t len)
327 if (ast_strlen_zero(data)) {
328 ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
332 pbx_substitute_variables_helper(chan, data, buf, len - 1);
337 static struct ast_custom_function eval_function = {
339 .synopsis = "Evaluate stored variables.",
340 .syntax = "EVAL(<variable>)",
341 .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
342 "When a variable or expression is in the dialplan, it will be\n"
343 "evaluated at runtime. However, if the result of the evaluation\n"
344 "is in fact a variable or expression, using EVAL will have it\n"
345 "evaluated a second time. For example, if the variable ${MYVAR}\n"
346 "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
347 "in the dialplan will be the contents of the variable, OTHERVAR.\n"
348 "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
349 "left with \"${OTHERVAR}\".\n",
350 .read = function_eval,
353 static char *tdesc = "String handling dialplan functions";
355 int unload_module(void)
359 res |= ast_custom_function_unregister(&fieldqty_function);
360 res |= ast_custom_function_unregister(&filter_function);
361 res |= ast_custom_function_unregister(®ex_function);
362 res |= ast_custom_function_unregister(&array_function);
363 res |= ast_custom_function_unregister(&len_function);
364 res |= ast_custom_function_unregister(&strftime_function);
365 res |= ast_custom_function_unregister(&strptime_function);
366 res |= ast_custom_function_unregister(&eval_function);
371 int load_module(void)
375 res |= ast_custom_function_register(&fieldqty_function);
376 res |= ast_custom_function_register(&filter_function);
377 res |= ast_custom_function_register(®ex_function);
378 res |= ast_custom_function_register(&array_function);
379 res |= ast_custom_function_register(&len_function);
380 res |= ast_custom_function_register(&strftime_function);
381 res |= ast_custom_function_register(&strptime_function);
382 res |= ast_custom_function_register(&eval_function);
387 char *description(void)
399 return ASTERISK_GPL_KEY;