Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.205
diff -u -u -r1.205 pbx.c
--- pbx.c	21 Feb 2005 06:31:01 -0000	1.205
+++ pbx.c	21 Feb 2005 18:34:23 -0000
@@ -11,6 +11,8 @@
  * the GNU General Public License
  */
 
+#include <sys/types.h>
+#include <regex.h>
 #include <asterisk/lock.h>
 #include <asterisk/cli.h>
 #include <asterisk/pbx.h>
@@ -1057,6 +1059,69 @@
 	}
 }
 
+
+#define lose_leading_spaces(str) while (str && str[0] == ' ') str++;
+
+static char *ast_func(char *in) {
+	char *out = NULL;
+	const char *ret_true = "1", *ret_false = "0";
+	if ((out = strchr(in, ' '))) {
+		char f_isnull = 0, f_exists = 0, f_contains = 0, f_begins = 0, f_ends=0, f_regex = 0;
+		*out = '\0';
+		out++;
+		if (! ( (f_isnull = !strcasecmp(in, "isnull"))
+				|| (f_exists = !strcasecmp(in, "exists"))
+				|| (f_contains = !strcasecmp(in, "contains"))
+				|| (f_begins = !strcasecmp(in, "begins"))
+				|| (f_ends = !strcasecmp(in, "ends"))
+				|| (f_regex = !strcasecmp(in, "regex"))
+				)) 
+			/* why bother */
+			return NULL;
+		
+		lose_leading_spaces(out);
+		if (f_isnull) {
+			return out ? strdup(ret_false) : strdup(ret_true);
+		} else if (f_exists) {
+			return out ? strdup(ret_true) : strdup(ret_false);
+		} else if (f_contains || f_begins || f_ends || f_regex) { /* funcs with an arg eg $(contains "foo" foo bar) */
+			char *arg, *tmp;
+			if ((tmp = ast_strdupa(out)) && (arg = strchr(tmp, '"')) && (out = strchr(arg+1, '"'))) {
+				arg++;
+				*out = '\0';
+				out++;
+				lose_leading_spaces(out);
+				if(f_contains) {
+					return strstr(out, arg) ? strdup(ret_true) : strdup(ret_false);				
+				} else if(f_begins) {
+					return strncmp(out, arg, strlen(arg)) ? strdup(ret_false) : strdup(ret_true);
+				} else if(f_ends) {
+					return (strcmp((out + strlen(out)) - strlen(arg), arg) ) ? strdup(ret_false) : strdup(ret_true);
+				} else if(f_regex) {
+					char *ret;
+					regex_t regexbuf;
+					if (regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB)) {
+						ast_log(LOG_WARNING, "Malformed regex input %s\n",in);
+						return NULL;
+					}
+					ret = regexec(&regexbuf, out, 0, NULL, 0) ? strdup(ret_false) : strdup(ret_true);
+					regfree(&regexbuf);
+					return ret;
+				}
+				
+			} else {
+				ast_log(LOG_WARNING, "Malformed input %s\n",in);
+			}
+			return NULL;
+		}
+
+	} else {
+		ast_log(LOG_WARNING, "Malformed input %s\n",in);
+	}
+
+	return NULL;
+}
+
 static void pbx_substitute_variables_helper_full(struct ast_channel *c, const char *cp1, char *cp2, int count, struct varshead *headp)
 {
 	char *cp4;
@@ -1064,9 +1129,9 @@
 	int length;
 	char workspace[4096];
 	char ltmp[4096], var[4096];
-	char *nextvar, *nextexp;
+	char *nextvar, *nextexp, *nextfunc;
 	char *vars, *vare;
-	int pos, brackets, needsub, len;
+	int pos, brackets, needsub, len, needfunc;
 	
 	/* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
 	   zero-filled */
@@ -1080,13 +1145,27 @@
 		
 		/* Look for an expression */
 		nextexp = strstr(whereweare, "$[");
+
+		/* Look for a function */
+		nextfunc = strstr(whereweare, "$(");
 		
 		/* Pick the first one only */
-		if (nextvar && nextexp) {
-			if (nextvar < nextexp)
-				nextexp = NULL;
-			else
-				nextvar = NULL;
+		len = 0;
+		if (nextvar)
+			len++;
+		if(nextexp)
+			len++;
+		if(nextfunc)
+			len++;
+
+		if (len > 1) {
+			if(nextfunc) {
+				nextvar = nextexp = NULL;
+			} else if (nextvar) {
+                nextexp = nextfunc = NULL;
+			} else if (nextexp) {
+                nextvar = nextfunc = NULL;
+            }
 		}
 		
 		/* If there is one, we only go that far */
@@ -1094,7 +1173,9 @@
 			pos = nextvar - whereweare;
 		else if (nextexp)
 			pos = nextexp - whereweare;
-		
+		else if (nextfunc) {
+			pos = nextfunc - whereweare;
+		}
 		/* Can't copy more than 'count' bytes */
 		if (pos > count)
 			pos = count;
@@ -1165,7 +1246,8 @@
 			vars = vare = nextexp + 2;
 			brackets = 1;
 			needsub = 0;
-			
+			needfunc = 0;
+
 			/* Find the end of it */
 			while(brackets && *vare) {
 				if ((vare[0] == '$') && (vare[1] == '[')) {
@@ -1179,6 +1261,9 @@
 				} else if ((vare[0] == '$') && (vare[1] == '{')) {
 					needsub++;
 					vare++;
+				} else if ((vare[0] == '$') && (vare[1] == '(')) {
+					needfunc++;
+					vare++;
 				}
 				vare++;
 			}
@@ -1195,7 +1280,7 @@
 			var[len] = '\0';
 			
 			/* Substitute if necessary */
-			if (needsub) {
+			if (needsub || needfunc) {
 				memset(ltmp, 0, sizeof(ltmp));
 				pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
 				vars = ltmp;
@@ -1217,6 +1302,63 @@
 				cp2 += length;
 				free(cp4);
 			}
+		} else if (nextfunc) {
+			/* We have a function.  Find the start and end */
+			vars = vare = nextfunc + 2;
+			brackets = 1;
+			needsub = 0;
+			
+			/* Find the end of it */
+			while(brackets && *vare) {
+				if ((vare[0] == '$') && (vare[1] == '(')) {
+					needsub++;
+					brackets++;
+					vare++;
+				} else if (vare[0] == '(') {
+					brackets++;
+				} else if (vare[0] == ')') {
+					brackets--;
+				} else if ((vare[0] == '$') && (vare[1] == '{')) {
+					needsub++;
+					vare++;
+				}
+				vare++;
+			}
+			if (brackets)
+				ast_log(LOG_NOTICE, "Error in extension logic (missing ')')\n");
+			len = vare - vars - 1;
+			
+			/* Skip totally over variable name */
+			whereweare += ( len + 3);
+			
+			/* Store variable name (and truncate) */
+			memset(var, 0, sizeof(var));
+			strncpy(var, vars, sizeof(var) - 1);
+			var[len] = '\0';
+			
+			/* Substitute if necessary */
+			if (needsub) {
+				memset(ltmp, 0, sizeof(ltmp));
+				pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
+				vars = ltmp;
+			} else {
+				vars = var;
+			}
+
+			/* Evaluate expression */			
+			cp4 = ast_func(vars);
+			
+			ast_log(LOG_WARNING, "Expression is '%s'\n", cp4);
+			
+			if (cp4) {
+				length = strlen(cp4);
+				if (length > count)
+					length = count;
+				memcpy(cp2, cp4, length);
+				count -= length;
+				cp2 += length;
+				free(cp4);
+			}
 			
 		} else
 			break;
@@ -1238,7 +1380,7 @@
 	memset(passdata, 0, datalen);
 		
 	/* No variables or expressions in e->data, so why scan it? */
-	if (!strstr(e->data,"${") && !strstr(e->data,"$[")) {
+	if (!strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
 		strncpy(passdata, e->data, datalen - 1);
 		passdata[datalen-1] = '\0';
 		return;
