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 earg = ++arg;
00093 strsep(&earg, "\"");
00094 if (earg) {
00095
00096 while (*earg == ' ')
00097 earg++;
00098 }
00099 } else {
00100 arg = tmp;
00101 }
00102
00103 if ((errcode = regcomp(®exbuf, arg, REG_EXTENDED | REG_NOSUB))) {
00104 regerror(errcode, ®exbuf, errstr, sizeof(errstr));
00105 ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
00106 } else {
00107 if (!regexec(®exbuf, earg ? earg : "", 0, NULL, 0))
00108 ast_copy_string(buf, "1", len);
00109 }
00110 regfree(®exbuf);
00111
00112 return buf;
00113 }
00114
00115 #ifndef BUILTIN_FUNC
00116 static
00117 #endif
00118 struct ast_custom_function regex_function = {
00119 .name = "REGEX",
00120 .synopsis = "Regular Expression: Returns 1 if data matches regular expression.",
00121 .syntax = "REGEX(\"<regular expression>\" <data>)",
00122 .read = builtin_function_regex,
00123 };
00124
00125 static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00126 {
00127 int length = 0;
00128 if (data) {
00129 length = strlen(data);
00130 }
00131 snprintf(buf, len, "%d", length);
00132 return buf;
00133 }
00134
00135 #ifndef BUILTIN_FUNC
00136 static
00137 #endif
00138 struct ast_custom_function len_function = {
00139 .name = "LEN",
00140 .synopsis = "Returns the length of the argument given",
00141 .syntax = "LEN(<string>)",
00142 .read = builtin_function_len,
00143 };
00144
00145 static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00146 {
00147 char *format, *epoch, *timezone = NULL;
00148 long epochi;
00149 struct tm time;
00150
00151 buf[0] = '\0';
00152
00153 if (!data) {
00154 ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
00155 return buf;
00156 }
00157
00158 format = ast_strdupa(data);
00159 if (!format) {
00160 ast_log(LOG_ERROR, "Out of memory\n");
00161 return buf;
00162 }
00163
00164 epoch = strsep(&format, "|");
00165 timezone = strsep(&format, "|");
00166
00167 if (ast_strlen_zero(epoch) || !sscanf(epoch, "%ld", &epochi)) {
00168 struct timeval tv = ast_tvnow();
00169 epochi = tv.tv_sec;
00170 }
00171
00172 ast_localtime(&epochi, &time, timezone);
00173
00174 if (!format) {
00175 format = "%c";
00176 }
00177
00178 if (!strftime(buf, len, format, &time)) {
00179 ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00180 }
00181 buf[len - 1] = '\0';
00182
00183 return buf;
00184 }
00185
00186 #ifndef BUILTIN_FUNC
00187 static
00188 #endif
00189 struct ast_custom_function strftime_function = {
00190 .name = "STRFTIME",
00191 .synopsis = "Returns the current date/time in a specified format.",
00192 .syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
00193 .read = acf_strftime,
00194 };
00195
00196 static char *function_eval(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00197 {
00198 memset(buf, 0, len);
00199
00200 if (ast_strlen_zero(data)) {
00201 ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00202 return buf;
00203 }
00204
00205 pbx_substitute_variables_helper(chan, data, buf, len - 1);
00206
00207 return buf;
00208 }
00209
00210 #ifndef BUILTIN_FUNC
00211 static
00212 #endif
00213 struct ast_custom_function eval_function = {
00214 .name = "EVAL",
00215 .synopsis = "Evaluate stored variables.",
00216 .syntax = "EVAL(<variable>)",
00217 .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
00218 "When a variable or expression is in the dialplan, it will be\n"
00219 "evaluated at runtime. However, if the result of the evaluation\n"
00220 "is in fact a variable or expression, using EVAL will have it\n"
00221 "evaluated a second time. For example, if the variable ${MYVAR}\n"
00222 "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
00223 "in the dialplan will be the contents of the variable, OTHERVAR.\n"
00224 "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
00225 "left with \"${OTHERVAR}\".\n",
00226 .read = function_eval,
00227 };
00228