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 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <sys/types.h>
00029 #include <regex.h>
00030
00031 #include "asterisk.h"
00032
00033
00034
00035 #include "asterisk/channel.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040 #include "asterisk/localtime.h"
00041
00042 static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00043 {
00044 char *varname, *varval, workspace[256];
00045 char *delim = ast_strdupa(data);
00046 int fieldcount = 0;
00047
00048 if (delim) {
00049 varname = strsep(&delim, "|");
00050 pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL);
00051 if (delim) {
00052 while (strsep(&varval, delim))
00053 fieldcount++;
00054 } else if (!ast_strlen_zero(varval)) {
00055 fieldcount = 1;
00056 }
00057 snprintf(buf, len, "%d", fieldcount);
00058 } else {
00059 ast_log(LOG_ERROR, "Out of memory\n");
00060 strncpy(buf, "1", len);
00061 }
00062 return buf;
00063 }
00064
00065 #ifndef BUILTIN_FUNC
00066 static
00067 #endif
00068 struct ast_custom_function fieldqty_function = {
00069 .name = "FIELDQTY",
00070 .synopsis = "Count the fields, with an arbitrary delimiter",
00071 .syntax = "FIELDQTY(<varname>,<delim>)",
00072 .read = function_fieldqty,
00073 };
00074
00075 static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00076 {
00077 char *arg, *earg = NULL, *tmp, errstr[256] = "";
00078 int errcode;
00079 regex_t regexbuf;
00080
00081 ast_copy_string(buf, "0", len);
00082
00083 tmp = ast_strdupa(data);
00084 if (!tmp) {
00085 ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
00086 return buf;
00087 }
00088
00089
00090 arg = strchr(tmp, '"');
00091 if (arg) {
00092 arg++;
00093 earg = strrchr(arg, '"');
00094 if (earg) {
00095 *earg++ = '\0';
00096
00097 while (*earg == ' ')
00098 earg++;
00099 }
00100 } else {
00101 arg = tmp;
00102 }
00103
00104 if ((errcode = regcomp(®exbuf, arg, REG_EXTENDED | REG_NOSUB))) {
00105 regerror(errcode, ®exbuf, errstr, sizeof(errstr));
00106 ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
00107 } else {
00108 if (!regexec(®exbuf, earg ? earg : "", 0, NULL, 0))
00109 ast_copy_string(buf, "1", len);
00110 }
00111 regfree(®exbuf);
00112
00113 return buf;
00114 }
00115
00116 #ifndef BUILTIN_FUNC
00117 static
00118 #endif
00119 struct ast_custom_function regex_function = {
00120 .name = "REGEX",
00121 .synopsis = "Regular Expression: Returns 1 if data matches regular expression.",
00122 .syntax = "REGEX(\"<regular expression>\" <data>)",
00123 .read = builtin_function_regex,
00124 };
00125
00126 static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00127 {
00128 int length = 0;
00129 if (data) {
00130 length = strlen(data);
00131 }
00132 snprintf(buf, len, "%d", length);
00133 return buf;
00134 }
00135
00136 #ifndef BUILTIN_FUNC
00137 static
00138 #endif
00139 struct ast_custom_function len_function = {
00140 .name = "LEN",
00141 .synopsis = "Returns the length of the argument given",
00142 .syntax = "LEN(<string>)",
00143 .read = builtin_function_len,
00144 };
00145
00146 static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00147 {
00148 char *format, *epoch, *timezone = NULL;
00149 long epochi;
00150 struct tm time;
00151
00152 buf[0] = '\0';
00153
00154 if (!data) {
00155 ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
00156 return buf;
00157 }
00158
00159 format = ast_strdupa(data);
00160 if (!format) {
00161 ast_log(LOG_ERROR, "Out of memory\n");
00162 return buf;
00163 }
00164
00165 epoch = strsep(&format, "|");
00166 timezone = strsep(&format, "|");
00167
00168 if (ast_strlen_zero(epoch) || !sscanf(epoch, "%ld", &epochi)) {
00169 struct timeval tv = ast_tvnow();
00170 epochi = tv.tv_sec;
00171 }
00172
00173 ast_localtime(&epochi, &time, timezone);
00174
00175 if (!format) {
00176 format = "%c";
00177 }
00178
00179 if (!strftime(buf, len, format, &time)) {
00180 ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00181 }
00182 buf[len - 1] = '\0';
00183
00184 return buf;
00185 }
00186
00187 #ifndef BUILTIN_FUNC
00188 static
00189 #endif
00190 struct ast_custom_function strftime_function = {
00191 .name = "STRFTIME",
00192 .synopsis = "Returns the current date/time in a specified format.",
00193 .syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
00194 .read = acf_strftime,
00195 };
00196
00197 static char *function_eval(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00198 {
00199 memset(buf, 0, len);
00200
00201 if (ast_strlen_zero(data)) {
00202 ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00203 return buf;
00204 }
00205
00206 pbx_substitute_variables_helper(chan, data, buf, len - 1);
00207
00208 return buf;
00209 }
00210
00211 #ifndef BUILTIN_FUNC
00212 static
00213 #endif
00214 struct ast_custom_function eval_function = {
00215 .name = "EVAL",
00216 .synopsis = "Evaluate stored variables.",
00217 .syntax = "EVAL(<variable>)",
00218 .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
00219 "When a variable or expression is in the dialplan, it will be\n"
00220 "evaluated at runtime. However, if the result of the evaluation\n"
00221 "is in fact a variable or expression, using EVAL will have it\n"
00222 "evaluated a second time. For example, if the variable ${MYVAR}\n"
00223 "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
00224 "in the dialplan will be the contents of the variable, OTHERVAR.\n"
00225 "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
00226 "left with \"${OTHERVAR}\".\n",
00227 .read = function_eval,
00228 };
00229