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