00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211596 $")
00030
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 #include <regex.h>
00036
00037 #include "asterisk/module.h"
00038 #include "asterisk/options.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/app.h"
00044 #include "asterisk/localtime.h"
00045
00046 static int function_fieldqty(struct ast_channel *chan, char *cmd,
00047 char *parse, char *buf, size_t len)
00048 {
00049 char *varsubst, varval[8192] = "", *varval2 = varval;
00050 int fieldcount = 0;
00051 AST_DECLARE_APP_ARGS(args,
00052 AST_APP_ARG(varname);
00053 AST_APP_ARG(delim);
00054 );
00055
00056 AST_STANDARD_APP_ARGS(args, parse);
00057 if (args.delim) {
00058 varsubst = alloca(strlen(args.varname) + 4);
00059
00060 sprintf(varsubst, "${%s}", args.varname);
00061 pbx_substitute_variables_helper(chan, varsubst, varval, sizeof(varval) - 1);
00062 if (ast_strlen_zero(varval2))
00063 fieldcount = 0;
00064 else {
00065 while (strsep(&varval2, args.delim))
00066 fieldcount++;
00067 }
00068 } else {
00069 fieldcount = 1;
00070 }
00071 snprintf(buf, len, "%d", fieldcount);
00072
00073 return 0;
00074 }
00075
00076 static struct ast_custom_function fieldqty_function = {
00077 .name = "FIELDQTY",
00078 .synopsis = "Count the fields, with an arbitrary delimiter",
00079 .syntax = "FIELDQTY(<varname>|<delim>)",
00080 .read = function_fieldqty,
00081 };
00082
00083 static int filter(struct ast_channel *chan, char *cmd, char *parse, char *buf,
00084 size_t len)
00085 {
00086 AST_DECLARE_APP_ARGS(args,
00087 AST_APP_ARG(allowed);
00088 AST_APP_ARG(string);
00089 );
00090 char *outbuf = buf;
00091
00092 AST_STANDARD_APP_ARGS(args, parse);
00093
00094 if (!args.string) {
00095 ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>|<string>)\n");
00096 return -1;
00097 }
00098
00099 for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
00100 if (strchr(args.allowed, *(args.string)))
00101 *outbuf++ = *(args.string);
00102 }
00103 *outbuf = '\0';
00104
00105 return 0;
00106 }
00107
00108 static struct ast_custom_function filter_function = {
00109 .name = "FILTER",
00110 .synopsis = "Filter the string to include only the allowed characters",
00111 .syntax = "FILTER(<allowed-chars>|<string>)",
00112 .read = filter,
00113 };
00114
00115 static int regex(struct ast_channel *chan, char *cmd, char *parse, char *buf,
00116 size_t len)
00117 {
00118 AST_DECLARE_APP_ARGS(args,
00119 AST_APP_ARG(null);
00120 AST_APP_ARG(reg);
00121 AST_APP_ARG(str);
00122 );
00123 int errcode;
00124 regex_t regexbuf;
00125
00126 buf[0] = '\0';
00127
00128 AST_NONSTANDARD_APP_ARGS(args, parse, '"');
00129
00130 if (args.argc != 3) {
00131 ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
00132 return -1;
00133 }
00134 if ((*args.str == ' ') || (*args.str == '\t'))
00135 args.str++;
00136
00137 if (option_debug)
00138 ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
00139
00140 if ((errcode = regcomp(®exbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
00141 regerror(errcode, ®exbuf, buf, len);
00142 ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
00143 return -1;
00144 }
00145
00146 strcpy(buf, regexec(®exbuf, args.str, 0, NULL, 0) ? "0" : "1");
00147
00148 regfree(®exbuf);
00149
00150 return 0;
00151 }
00152
00153 static struct ast_custom_function regex_function = {
00154 .name = "REGEX",
00155 .synopsis = "Regular Expression",
00156 .desc =
00157 "Returns 1 if data matches regular expression, or 0 otherwise.\n"
00158 "Please note that the space following the double quotes separating the regex from the data\n"
00159 "is optional and if present, is skipped. If a space is desired at the beginning of the data,\n"
00160 "then put two spaces there; the second will not be skipped.\n",
00161 .syntax = "REGEX(\"<regular expression>\" <data>)",
00162 .read = regex,
00163 };
00164
00165 static int array(struct ast_channel *chan, char *cmd, char *var,
00166 const char *value)
00167 {
00168 AST_DECLARE_APP_ARGS(arg1,
00169 AST_APP_ARG(var)[100];
00170 );
00171 AST_DECLARE_APP_ARGS(arg2,
00172 AST_APP_ARG(val)[100];
00173 );
00174 char *value2;
00175 int i;
00176
00177 value2 = ast_strdupa(value);
00178 if (!var || !value2)
00179 return -1;
00180
00181
00182
00183
00184
00185
00186
00187 if (option_debug)
00188 ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
00189 if (strchr(var, ','))
00190 AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
00191 else
00192 AST_STANDARD_APP_ARGS(arg1, var);
00193
00194 if (strchr(value2, ','))
00195 AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
00196 else
00197 AST_STANDARD_APP_ARGS(arg2, value2);
00198
00199 for (i = 0; i < arg1.argc; i++) {
00200 if (option_debug)
00201 ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
00202 arg2.val[i]);
00203 if (i < arg2.argc) {
00204 pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
00205 } else {
00206
00207
00208 pbx_builtin_setvar_helper(chan, arg1.var[i], "");
00209 }
00210 }
00211
00212 return 0;
00213 }
00214
00215 static struct ast_custom_function array_function = {
00216 .name = "ARRAY",
00217 .synopsis = "Allows setting multiple variables at once",
00218 .syntax = "ARRAY(var1[|var2[...][|varN]])",
00219 .write = array,
00220 .desc =
00221 "The comma-separated list passed as a value to which the function is set will\n"
00222 "be interpreted as a set of values to which the comma-separated list of\n"
00223 "variable names in the argument should be set.\n"
00224 "Hence, Set(ARRAY(var1|var2)=1\\,2) will set var1 to 1 and var2 to 2\n"
00225 "Note: remember to either backslash your commas in extensions.conf or quote the\n"
00226 "entire argument, since Set can take multiple arguments itself.\n",
00227 };
00228
00229 static int acf_sprintf(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00230 {
00231 #define SPRINTF_FLAG 0
00232 #define SPRINTF_WIDTH 1
00233 #define SPRINTF_PRECISION 2
00234 #define SPRINTF_LENGTH 3
00235 #define SPRINTF_CONVERSION 4
00236 int i, state = -1, argcount = 0;
00237 char *formatstart = NULL, *bufptr = buf;
00238 char formatbuf[256] = "";
00239 int tmpi;
00240 double tmpd;
00241 AST_DECLARE_APP_ARGS(arg,
00242 AST_APP_ARG(format);
00243 AST_APP_ARG(var)[100];
00244 );
00245
00246 AST_STANDARD_APP_ARGS(arg, data);
00247
00248
00249 for (i = 0; arg.format[i]; i++) {
00250 switch (state) {
00251 case SPRINTF_FLAG:
00252 if (strchr("#0- +'I", arg.format[i]))
00253 break;
00254 state = SPRINTF_WIDTH;
00255 case SPRINTF_WIDTH:
00256 if (arg.format[i] >= '0' && arg.format[i] <= '9')
00257 break;
00258
00259
00260 if (arg.format[i] == '.') {
00261 state = SPRINTF_PRECISION;
00262 } else {
00263 state = SPRINTF_LENGTH;
00264 i--;
00265 }
00266 break;
00267 case SPRINTF_PRECISION:
00268 if (arg.format[i] >= '0' && arg.format[i] <= '9')
00269 break;
00270 state = SPRINTF_LENGTH;
00271 case SPRINTF_LENGTH:
00272 if (strchr("hl", arg.format[i])) {
00273 if (arg.format[i + 1] == arg.format[i])
00274 i++;
00275 state = SPRINTF_CONVERSION;
00276 break;
00277 } else if (strchr("Lqjzt", arg.format[i])) {
00278 state = SPRINTF_CONVERSION;
00279 break;
00280 }
00281 state = SPRINTF_CONVERSION;
00282 case SPRINTF_CONVERSION:
00283 if (strchr("diouxXc", arg.format[i])) {
00284
00285
00286
00287 ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00288 formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00289
00290
00291 if (arg.var[argcount]) {
00292 if (sscanf(arg.var[argcount++], "%30d", &tmpi) != 1) {
00293 ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00294 goto sprintf_fail;
00295 }
00296 } else {
00297 ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00298 goto sprintf_fail;
00299 }
00300
00301
00302 snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi);
00303
00304
00305 bufptr = strchr(buf, '\0');
00306 } else if (strchr("eEfFgGaA", arg.format[i])) {
00307
00308
00309
00310 ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00311 formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00312
00313
00314 if (arg.var[argcount]) {
00315 if (sscanf(arg.var[argcount++], "%30lf", &tmpd) != 1) {
00316 ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00317 goto sprintf_fail;
00318 }
00319 } else {
00320 ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00321 goto sprintf_fail;
00322 }
00323
00324
00325 snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd);
00326
00327
00328 bufptr = strchr(buf, '\0');
00329 } else if (arg.format[i] == 's') {
00330
00331
00332
00333 ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00334 formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00335
00336
00337 snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]);
00338
00339
00340 bufptr = strchr(buf, '\0');
00341 } else if (arg.format[i] == '%') {
00342
00343 *bufptr++ = arg.format[i];
00344 } else {
00345
00346
00347
00348 ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00349 formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00350
00351 ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]);
00352 goto sprintf_fail;
00353 }
00354 state = -1;
00355 break;
00356 default:
00357 if (arg.format[i] == '%') {
00358 state = SPRINTF_FLAG;
00359 formatstart = &arg.format[i];
00360 break;
00361 } else {
00362
00363 *bufptr++ = arg.format[i];
00364 }
00365 }
00366 }
00367 *bufptr = '\0';
00368 return 0;
00369 sprintf_fail:
00370 return -1;
00371 }
00372
00373 static struct ast_custom_function sprintf_function = {
00374 .name = "SPRINTF",
00375 .synopsis = "Format a variable according to a format string",
00376 .syntax = "SPRINTF(<format>|<arg1>[|...<argN>])",
00377 .read = acf_sprintf,
00378 .desc =
00379 "Parses the format string specified and returns a string matching that format.\n"
00380 "Supports most options supported by sprintf(3). Returns a shortened string if\n"
00381 "a format specifier is not recognized.\n",
00382 };
00383
00384 static int quote(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00385 {
00386 char *bufptr = buf, *dataptr = data;
00387 *bufptr++ = '"';
00388 for (; bufptr < buf + len - 1; dataptr++) {
00389 if (*dataptr == '\\') {
00390 *bufptr++ = '\\';
00391 *bufptr++ = '\\';
00392 } else if (*dataptr == '"') {
00393 *bufptr++ = '\\';
00394 *bufptr++ = '"';
00395 } else if (*dataptr == '\0') {
00396 break;
00397 } else {
00398 *bufptr++ = *dataptr;
00399 }
00400 }
00401 *bufptr++ = '"';
00402 *bufptr = '\0';
00403 return 0;
00404 }
00405
00406 static struct ast_custom_function quote_function = {
00407 .name = "QUOTE",
00408 .synopsis = "Quotes a given string, escaping embedded quotes as necessary",
00409 .syntax = "QUOTE(<string>)",
00410 .read = quote,
00411 };
00412
00413
00414 static int len(struct ast_channel *chan, char *cmd, char *data, char *buf,
00415 size_t len)
00416 {
00417 int length = 0;
00418
00419 if (data)
00420 length = strlen(data);
00421
00422 snprintf(buf, len, "%d", length);
00423
00424 return 0;
00425 }
00426
00427 static struct ast_custom_function len_function = {
00428 .name = "LEN",
00429 .synopsis = "Returns the length of the argument given",
00430 .syntax = "LEN(<string>)",
00431 .read = len,
00432 };
00433
00434 static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
00435 char *buf, size_t len)
00436 {
00437 AST_DECLARE_APP_ARGS(args,
00438 AST_APP_ARG(epoch);
00439 AST_APP_ARG(timezone);
00440 AST_APP_ARG(format);
00441 );
00442 time_t epochi;
00443 struct tm tm;
00444
00445 buf[0] = '\0';
00446
00447 AST_STANDARD_APP_ARGS(args, parse);
00448
00449 ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
00450 ast_localtime(&epochi, &tm, args.timezone);
00451
00452 if (!args.format)
00453 args.format = "%c";
00454
00455 if (!strftime(buf, len, args.format, &tm))
00456 ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00457
00458 buf[len - 1] = '\0';
00459
00460 return 0;
00461 }
00462
00463 static struct ast_custom_function strftime_function = {
00464 .name = "STRFTIME",
00465 .synopsis = "Returns the current date/time in a specified format.",
00466 .syntax = "STRFTIME([<epoch>][|[timezone][|format]])",
00467 .read = acf_strftime,
00468 };
00469
00470 static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
00471 char *buf, size_t len)
00472 {
00473 AST_DECLARE_APP_ARGS(args,
00474 AST_APP_ARG(timestring);
00475 AST_APP_ARG(timezone);
00476 AST_APP_ARG(format);
00477 );
00478 struct tm time;
00479
00480 memset(&time, 0, sizeof(struct tm));
00481
00482 buf[0] = '\0';
00483
00484 if (!data) {
00485 ast_log(LOG_ERROR,
00486 "Asterisk function STRPTIME() requires an argument.\n");
00487 return -1;
00488 }
00489
00490 AST_STANDARD_APP_ARGS(args, data);
00491
00492 if (ast_strlen_zero(args.format)) {
00493 ast_log(LOG_ERROR,
00494 "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
00495 return -1;
00496 }
00497
00498 if (!strptime(args.timestring, args.format, &time)) {
00499 ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
00500 } else {
00501
00502 time.tm_isdst = -1;
00503 snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
00504 }
00505
00506 return 0;
00507 }
00508
00509 static struct ast_custom_function strptime_function = {
00510 .name = "STRPTIME",
00511 .synopsis =
00512 "Returns the epoch of the arbitrary date/time string structured as described in the format.",
00513 .syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
00514 .desc =
00515 "This is useful for converting a date into an EPOCH time, possibly to pass to\n"
00516 "an application like SayUnixTime or to calculate the difference between two\n"
00517 "date strings.\n"
00518 "\n"
00519 "Example:\n"
00520 " ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
00521 .read = acf_strptime,
00522 };
00523
00524 static int function_eval(struct ast_channel *chan, char *cmd, char *data,
00525 char *buf, size_t len)
00526 {
00527 memset(buf, 0, len);
00528
00529 if (ast_strlen_zero(data)) {
00530 ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00531 return -1;
00532 }
00533
00534 pbx_substitute_variables_helper(chan, data, buf, len - 1);
00535
00536 return 0;
00537 }
00538
00539 static struct ast_custom_function eval_function = {
00540 .name = "EVAL",
00541 .synopsis = "Evaluate stored variables.",
00542 .syntax = "EVAL(<variable>)",
00543 .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
00544 "When a variable or expression is in the dialplan, it will be\n"
00545 "evaluated at runtime. However, if the result of the evaluation\n"
00546 "is in fact a variable or expression, using EVAL will have it\n"
00547 "evaluated a second time. For example, if the variable ${MYVAR}\n"
00548 "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
00549 "in the dialplan will be the contents of the variable, OTHERVAR.\n"
00550 "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
00551 "left with \"${OTHERVAR}\".\n",
00552 .read = function_eval,
00553 };
00554
00555 static int keypadhash(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00556 {
00557 char *bufptr, *dataptr;
00558
00559 for (bufptr = buf, dataptr = data; bufptr < buf + len - 1; dataptr++) {
00560 if (*dataptr == '\0') {
00561 *bufptr++ = '\0';
00562 break;
00563 } else if (*dataptr == '1') {
00564 *bufptr++ = '1';
00565 } else if (strchr("AaBbCc2", *dataptr)) {
00566 *bufptr++ = '2';
00567 } else if (strchr("DdEeFf3", *dataptr)) {
00568 *bufptr++ = '3';
00569 } else if (strchr("GgHhIi4", *dataptr)) {
00570 *bufptr++ = '4';
00571 } else if (strchr("JjKkLl5", *dataptr)) {
00572 *bufptr++ = '5';
00573 } else if (strchr("MmNnOo6", *dataptr)) {
00574 *bufptr++ = '6';
00575 } else if (strchr("PpQqRrSs7", *dataptr)) {
00576 *bufptr++ = '7';
00577 } else if (strchr("TtUuVv8", *dataptr)) {
00578 *bufptr++ = '8';
00579 } else if (strchr("WwXxYyZz9", *dataptr)) {
00580 *bufptr++ = '9';
00581 } else if (*dataptr == '0') {
00582 *bufptr++ = '0';
00583 }
00584 }
00585 buf[len - 1] = '\0';
00586
00587 return 0;
00588 }
00589
00590 static struct ast_custom_function keypadhash_function = {
00591 .name = "KEYPADHASH",
00592 .synopsis = "Hash the letters in the string into the equivalent keypad numbers.",
00593 .syntax = "KEYPADHASH(<string>)",
00594 .read = keypadhash,
00595 .desc = "Example: ${KEYPADHASH(Les)} returns \"537\"\n",
00596 };
00597
00598 static int unload_module(void)
00599 {
00600 int res = 0;
00601
00602 res |= ast_custom_function_unregister(&fieldqty_function);
00603 res |= ast_custom_function_unregister(&filter_function);
00604 res |= ast_custom_function_unregister(®ex_function);
00605 res |= ast_custom_function_unregister(&array_function);
00606 res |= ast_custom_function_unregister("e_function);
00607 res |= ast_custom_function_unregister(&len_function);
00608 res |= ast_custom_function_unregister(&strftime_function);
00609 res |= ast_custom_function_unregister(&strptime_function);
00610 res |= ast_custom_function_unregister(&eval_function);
00611 res |= ast_custom_function_unregister(&keypadhash_function);
00612 res |= ast_custom_function_unregister(&sprintf_function);
00613
00614 return res;
00615 }
00616
00617 static int load_module(void)
00618 {
00619 int res = 0;
00620
00621 res |= ast_custom_function_register(&fieldqty_function);
00622 res |= ast_custom_function_register(&filter_function);
00623 res |= ast_custom_function_register(®ex_function);
00624 res |= ast_custom_function_register(&array_function);
00625 res |= ast_custom_function_register("e_function);
00626 res |= ast_custom_function_register(&len_function);
00627 res |= ast_custom_function_register(&strftime_function);
00628 res |= ast_custom_function_register(&strptime_function);
00629 res |= ast_custom_function_register(&eval_function);
00630 res |= ast_custom_function_register(&keypadhash_function);
00631 res |= ast_custom_function_register(&sprintf_function);
00632
00633 return res;
00634 }
00635
00636 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");