Skip to content
Snippets Groups Projects
func_strings.c 68.1 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Asterisk -- An open source telephony toolkit.
    
     * Copyright (C) 2005-2006, Digium, Inc.
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
     * Portions Copyright (C) 2005, Tilghman Lesher.  All rights reserved.
     * Portions Copyright (C) 2005, Anthony Minessale II
    
     * Portions Copyright (C) 2021, 2022, Naveen Albert
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     * \brief String manipulation dialplan functions
    
     *
     * \author Tilghman Lesher
    
    Josh Soref's avatar
    Josh Soref committed
     * \author Anthony Minessale II
    
     * \author Naveen Albert
    
    Olle Johansson's avatar
    Olle Johansson committed
     * \ingroup functions
    
    /*** MODULEINFO
    	<support_level>core</support_level>
     ***/
    
    
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/utils.h"
    #include "asterisk/app.h"
    
    #include "asterisk/test.h"
    
    /*** DOCUMENTATION
    	<function name="FIELDQTY" language="en_US">
    		<synopsis>
    			Count the fields with an arbitrary delimiter
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delim" required="true" />
    		</syntax>
    		<description>
    
    			<para>The delimiter may be specified as a special or extended ASCII character, by encoding it.  The characters
    			<literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
    			carriage return, and tab characters, respectively.  Also, octal and hexadecimal specifications are recognized
    			by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively.  For example, if you wanted
    			to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
    
    			<example title="Prints 3">
    			exten => s,1,Set(example=ex-amp-le)
    				same => n,NoOp(${FIELDQTY(example,-)})
    			</example>
    
    		</description>
    	</function>
    
    	<function name="FIELDNUM" language="en_US">
    		<synopsis>
    			Return the 1-based offset of a field in a list
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delim" required="true" />
    			<parameter name="value" required="true" />
    		</syntax>
    		<description>
    			<para>Search the variable named <replaceable>varname</replaceable> for the string <replaceable>value</replaceable>
    			delimited by <replaceable>delim</replaceable> and return a 1-based offset as to its location. If not found
    			or an error occured, return <literal>0</literal>.</para>
    			<para>The delimiter may be specified as a special or extended ASCII character, by encoding it.  The characters
    			<literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
    			carriage return, and tab characters, respectively.  Also, octal and hexadecimal specifications are recognized
    			by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively.  For example, if you wanted
    			to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
    
    			<example title="Prints 2">
    			exten => s,1,Set(example=ex-amp-le)
    				same => n,NoOp(${FIELDNUM(example,-,amp)})
    			</example>
    
    		</description>
    	</function>
    
    	<function name="LISTFILTER" language="en_US">
    		<synopsis>Remove an item from a list, by name.</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delim" required="true" default="," />
    			<parameter name="value" required="true" />
    		</syntax>
    		<description>
    			<para>Remove <replaceable>value</replaceable> from the list contained in the <replaceable>varname</replaceable>
    			variable, where the list delimiter is specified by the <replaceable>delim</replaceable> parameter.  This is
    			very useful for removing a single channel name from a list of channels, for example.</para>
    		</description>
    	</function>
    
    	<function name="FILTER" language="en_US">
    		<synopsis>
    			Filter the string to include only the allowed characters
    		</synopsis>
    		<syntax>
    			<parameter name="allowed-chars" required="true" />
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<para>Permits all characters listed in <replaceable>allowed-chars</replaceable>,
    			filtering all others outs. In addition to literally listing the characters,
    
    			you may also use ranges of characters (delimited by a <literal>-</literal></para>
    			<para>Hexadecimal characters started with a <literal>\x</literal>(i.e. \x20)</para>
    			<para>Octal characters started with a <literal>\0</literal> (i.e. \040)</para>
    
    			<para>Also <literal>\t</literal>,<literal>\n</literal> and <literal>\r</literal> are recognized.</para>
    			<note><para>If you want the <literal>-</literal> character it needs to be prefixed with a
    
    			<literal>\</literal></para></note>
    		</description>
    	</function>
    
    	<function name="REPLACE" language="en_US">
    		<synopsis>
    			Replace a set of characters in a given string with another character.
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="find-chars" required="true" />
    			<parameter name="replace-char" required="false" />
    		</syntax>
    		<description>
    			<para>Iterates through a string replacing all the <replaceable>find-chars</replaceable> with
    			<replaceable>replace-char</replaceable>.  <replaceable>replace-char</replaceable> may be either
    			empty or contain one character.  If empty, all <replaceable>find-chars</replaceable> will be
    			deleted from the output.</para>
    			<note><para>The replacement only occurs in the output.  The original variable is not
    			altered.</para></note>
    		</description>
    	</function>
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    	<function name="STRREPLACE" language="en_US">
    		<synopsis>
    			Replace instances of a substring within a string with another string.
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="find-string" required="true" />
    			<parameter name="replace-string" required="false" />
    			<parameter name="max-replacements" required="false" />
    		</syntax>
    		<description>
    			<para>Searches for all instances of the <replaceable>find-string</replaceable> in provided variable and
    			replaces them with <replaceable>replace-string</replaceable>.  If <replaceable>replace-string</replaceable>
    
    Josh Soref's avatar
    Josh Soref committed
    			is an empty string, this will effectively delete that substring.  If <replaceable>max-replacements</replaceable>
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    			is specified, this function will stop after performing replacements <replaceable>max-replacements</replaceable> times.</para>
    			<note><para>The replacement only occurs in the output.  The original variable is not altered.</para></note>
    		</description>
    	</function>
    
    	<function name="STRBETWEEN" language="en_US">
    
    		<since>
    			<version>16.21.0</version>
    			<version>18.7.0</version>
    			<version>19.0.0</version>
    		</since>
    
    		<synopsis>
    			Inserts a substring between each character in a string.
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="insert-string" required="true" />
    		</syntax>
    		<description>
    			<para>Inserts a substring <replaceable>find-string</replaceable> between each character in
    			<replaceable>varname</replaceable>.</para>
    			<note><para>The replacement only occurs in the output.  The original variable is not altered.</para></note>
    			<example title="Add half-second pause between dialed digits">
    				same => n,Set(digits=5551212)
    				same => n,SendDTMF(${STRBETWEEN(digits,w)) ; this will send 5w5w5w1w2w1w2
    			</example>
    		</description>
    	</function>
    
    	<function name="TRIM" language="en_US">
    		<synopsis>
    			Trim leading and trailing whitespace in a string
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    			<para>Replaces all leading and trailing whitespace in the provided string.</para>
    		</description>
    		<see-also>
    			<ref type="function">LTRIM</ref>
    			<ref type="function">RTRIM</ref>
    		</see-also>
    	</function>
    	<function name="LTRIM" language="en_US">
    		<synopsis>
    			Trim leading whitespace in a string
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    			<para>Replaces all leading whitespace in the provided string.</para>
    		</description>
    		<see-also>
    			<ref type="function">TRIM</ref>
    			<ref type="function">RTRIM</ref>
    		</see-also>
    	</function>
    	<function name="RTRIM" language="en_US">
    		<synopsis>
    			Trim trailing whitespace in a string
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    			<para>Replaces all trailing whitespace in the provided string.</para>
    		</description>
    		<see-also>
    			<ref type="function">TRIM</ref>
    			<ref type="function">LTRIM</ref>
    		</see-also>
    	</function>
    
    	<function name="PASSTHRU" language="en_US">
    		<synopsis>
    			Pass the given argument back as a value.
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="false" />
    		</syntax>
    		<description>
    			<para>Literally returns the given <replaceable>string</replaceable>.  The intent is to permit
    			other dialplan functions which take a variable name as an argument to be able to take a literal
    			string, instead.</para>
    
    			<note><para>The functions which take a variable name need to be passed var and not
    			${var}.  Similarly, use PASSTHRU() and not ${PASSTHRU()}.</para></note>
    
    			<example title="Prints 321">
    			exten => s,1,NoOp(${CHANNEL}) ; contains SIP/321-1
    				same => n,NoOp(${CUT(PASSTHRU(${CUT(CHANNEL,-,1)}),/,2)})
    			</example>
    
    	<function name="REGEX" language="en_US">
    		<synopsis>
    			Check string against a regular expression.
    		</synopsis>
    		<syntax argsep=" ">
    			<parameter name="&quot;regular expression&quot;" required="true" />
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    			<para>Return <literal>1</literal> on regular expression match or <literal>0</literal> otherwise</para>
    
    			<para>Please note that the space following the double quotes separating the
    			regex from the data is optional and if present, is skipped. If a space is
    			desired at the beginning of the data, then put two spaces there; the second
    
    			will not be skipped.</para>
    		</description>
    	</function>
    	<application name="ClearHash" language="en_US">
    		<synopsis>
    			Clear the keys from a specified hashname.
    		</synopsis>
    		<syntax>
    			<parameter name="hashname" required="true" />
    		</syntax>
    		<description>
    			<para>Clears all keys out of the specified <replaceable>hashname</replaceable>.</para>
    		</description>
    	</application>
    	<function name="HASH" language="en_US">
    		<synopsis>
    			Implementation of a dialplan associative array
    		</synopsis>
    		<syntax>
    			<parameter name="hashname" required="true" />
    			<parameter name="hashkey" />
    		</syntax>
    		<description>
    			<para>In two arguments mode, gets and sets values to corresponding keys within
    			a named associative array. The single-argument mode will only work when assigned
    			to from a function defined by func_odbc</para>
    		</description>
    	</function>
    	<function name="HASHKEYS" language="en_US">
    		<synopsis>
    			Retrieve the keys of the HASH() function.
    		</synopsis>
    		<syntax>
    			<parameter name="hashname" required="true" />
    		</syntax>
    		<description>
    
    			<para>Returns a comma-delimited list of the current keys of the associative array
    			defined by the HASH() function. Note that if you iterate over the keys of
    
    			the result, adding keys during iteration will cause the result of the HASHKEYS()
    			function to change.</para>
    		</description>
    	</function>
    	<function name="KEYPADHASH" language="en_US">
    		<synopsis>
    			Hash the letters in string into equivalent keypad numbers.
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<example title="Returns 537">
    			exten => s,1,Return(${KEYPADHASH(Les)})
    			</example>
    
    		</description>
    	</function>
    	<function name="ARRAY" language="en_US">
    		<synopsis>
    			Allows setting multiple variables at once.
    		</synopsis>
    		<syntax>
    			<parameter name="var1" required="true" />
    			<parameter name="var2" required="false" multiple="true" />
    			<parameter name="varN" required="false" />
    		</syntax>
    		<description>
    
    			<para>The comma-delimited list passed as a value to which the function is set will
    			be interpreted as a set of values to which the comma-delimited list of
    
    			variable names in the argument should be set.</para>
    
    			<example title="Set var1 to 1 and var2 to 2">
    			same => n,Set(ARRAY(var1,var2)=1,2)
    			</example>
    
    		</description>
    	</function>
    	<function name="STRPTIME" language="en_US">
    		<synopsis>
    			Returns the epoch of the arbitrary date/time string structured as described by the format.
    		</synopsis>
    		<syntax>
    			<parameter name="datetime" required="true" />
    			<parameter name="timezone" required="true" />
    			<parameter name="format" required="true" />
    		</syntax>
    		<description>
    
    			<para>This is useful for converting a date into <literal>EPOCH</literal> time,
    
    			possibly to pass to an application like SayUnixTime or to calculate the difference
    			between the two date strings</para>
    
    			<example title="Prints 1141219835">
    			same => n,NoOp(${STRPTIME(2006-03-01 07:30:35,America/Chicago,%Y-%m-%d %H:%M:%S)})
    			</example>
    
    		</description>
    	</function>
    	<function name="STRFTIME" language="en_US">
    		<synopsis>
    			Returns the current date/time in the specified format.
    		</synopsis>
    		<syntax>
    			<parameter name="epoch" />
    			<parameter name="timezone" />
    			<parameter name="format" />
    		</syntax>
    		<description>
    			<para>STRFTIME supports all of the same formats as the underlying C function
    			<emphasis>strftime(3)</emphasis>.
    			It also supports the following format: <literal>%[n]q</literal> - fractions of a second,
    			with leading zeros.</para>
    			<para>Example: <literal>%3q</literal> will give milliseconds and <literal>%1q</literal>
    			will give tenths of a second. The default is set at milliseconds (n=3).
    			The common case is to use it in combination with %S, as in <literal>%S.%3q</literal>.</para>
    		</description>
    		<see-also>
    			<ref type="manpage">strftime(3)</ref>
    		</see-also>
    	</function>
    	<function name="EVAL" language="en_US">
    		<synopsis>
    			Evaluate stored variables
    		</synopsis>
    		<syntax>
    			<parameter name="variable" required="true" />
    		</syntax>
    		<description>
    			<para>Using EVAL basically causes a string to be evaluated twice.
    			When a variable or expression is in the dialplan, it will be
    			evaluated at runtime. However, if the results of the evaluation
    			is in fact another variable or expression, using EVAL will have it
    			evaluated a second time.</para>
    			<para>Example: If the <variable>MYVAR</variable> contains
    			<variable>OTHERVAR</variable>, then the result of ${EVAL(
    			<variable>MYVAR</variable>)} in the dialplan will be the
    			contents of <variable>OTHERVAR</variable>. Normally just
    			putting <variable>MYVAR</variable> in the dialplan the result
    			would be <variable>OTHERVAR</variable>.</para>
    		</description>
    	</function>
    	<function name="TOUPPER" language="en_US">
    		<synopsis>
    			Convert string to all uppercase letters.
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<example title="Prints EXAMPLE">
    			exten => s,1,NoOp(${TOUPPER(Example)})
    			</example>
    
    		</description>
    	</function>
    	<function name="TOLOWER" language="en_US">
    		<synopsis>
    			Convert string to all lowercase letters.
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<example title="Prints example">
    			exten => s,1,NoOp(${TOLOWER(Example)})
    			</example>
    
    		</description>
    	</function>
    	<function name="LEN" language="en_US">
    		<synopsis>
    			Return the length of the string given.
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<example title="Prints 7">
    			exten => s,1,NoOp(${LEN(example)})
    			</example>
    
    		</description>
    	</function>
    	<function name="QUOTE" language="en_US">
    		<synopsis>
    			Quotes a given string, escaping embedded quotes as necessary
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    
    			<para>Example: ${QUOTE(ab"c"de)} will return ""ab\"c\"de""</para>
    
    		</description>
    	</function>
    
    	<function name="CSV_QUOTE" language="en_US">
    		<synopsis>
    			Quotes a given string for use in a CSV file, escaping embedded quotes as necessary
    		</synopsis>
    		<syntax>
    			<parameter name="string" required="true" />
    		</syntax>
    		<description>
    			<para>Example: ${CSV_QUOTE("a,b" 123)} will return """a,b"" 123"</para>
    		</description>
    	</function>
    
    	<function name="SHIFT" language="en_US">
    		<synopsis>
    			Removes and returns the first item off of a variable containing delimited text
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delimiter" required="false" default="," />
    		</syntax>
    		<description>
    
    			<example title="SHIFT example">
    			exten => s,1,Set(array=one,two,three)
    			exten => s,n,While($["${SET(var=${SHIFT(array)})}" != ""])
    			exten => s,n,NoOp(var is ${var})
    			exten => s,n,EndWhile
    			</example>
    
    			<para>This would iterate over each value in array, left to right, and
    				would result in NoOp(var is one), NoOp(var is two), and
    				NoOp(var is three) being executed.
    			</para>
    		</description>
    
    	<function name="POP" language="en_US">
    		<synopsis>
    			Removes and returns the last item off of a variable containing delimited text
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delimiter" required="false" default="," />
    		</syntax>
    		<description>
    
    			<example title="POP example">
    			exten => s,1,Set(array=one,two,three)
    			exten => s,n,While($["${SET(var=${POP(array)})}" != ""])
    			exten => s,n,NoOp(var is ${var})
    			exten => s,n,EndWhile
    			</example>
    
    			<para>This would iterate over each value in array, right to left, and
    				would result in NoOp(var is three), NoOp(var is two), and
    				NoOp(var is one) being executed.
    			</para>
    		</description>
    
    	<function name="PUSH" language="en_US">
    		<synopsis>
    			Appends one or more values to the end of a variable containing delimited text
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delimiter" required="false" default="," />
    		</syntax>
    		<description>
    
    			<example title="PUSH example">
    			exten => s,1,Set(PUSH(array)=one,two,three)
    			</example>
    			<para>This would append one,
    
    				two, and three to the end of the values stored in the variable
    				"array".
    			</para>
    		</description>
    	</function>
    	<function name="UNSHIFT" language="en_US">
    		<synopsis>
    			Inserts one or more values to the beginning of a variable containing delimited text
    		</synopsis>
    		<syntax>
    			<parameter name="varname" required="true" />
    			<parameter name="delimiter" required="false" default="," />
    		</syntax>
    		<description>
    
    			<example title="UNSHIFT example">
    			exten => s,1,Set(UNSHIFT(array)=one,two,three)
    			</example>
    			<para>This would insert one,
    
    				two, and three before the values stored in the variable
    				"array".
    			</para>
    		</description>
    	</function>
    
    static int function_fieldqty_helper(struct ast_channel *chan, const char *cmd,
    			     char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
    
    	char *varsubst;
    
    	struct ast_str *str = ast_str_thread_get(&result_buf, 16);
    
    	AST_DECLARE_APP_ARGS(args,
    
    			     AST_APP_ARG(varname);
    			     AST_APP_ARG(delim);
    		);
    
    	if (!str) {
    		return -1;
    	}
    
    
    	AST_STANDARD_APP_ARGS(args, parse);
    	if (args.delim) {
    
    		ast_get_encoded_char(args.delim, delim, &delim_used);
    
    
    		varsubst = ast_alloca(strlen(args.varname) + 4);
    
    		sprintf(varsubst, "${%s}", args.varname);
    
    		ast_str_substitute_variables(&str, 0, chan, varsubst);
    		if (ast_str_strlen(str) == 0) {
    
    		} else {
    			char *varval = ast_str_buffer(str);
    			while (strsep(&varval, delim)) {
    
    	if (sbuf) {
    		ast_str_set(sbuf, len, "%d", fieldcount);
    	} else {
    		snprintf(buf, len, "%d", fieldcount);
    	}
    
    	return 0;
    
    static int function_fieldqty(struct ast_channel *chan, const char *cmd,
    			     char *parse, char *buf, size_t len)
    {
    	return function_fieldqty_helper(chan, cmd, parse, buf, NULL, len);
    }
    
    static int function_fieldqty_str(struct ast_channel *chan, const char *cmd,
    				 char *parse, struct ast_str **buf, ssize_t len)
    {
    	return function_fieldqty_helper(chan, cmd, parse, NULL, buf, len);
    }
    
    
    static struct ast_custom_function fieldqty_function = {
    
    	.name = "FIELDQTY",
    	.read = function_fieldqty,
    
    	.read2 = function_fieldqty_str,
    
    static int function_fieldnum_helper(struct ast_channel *chan, const char *cmd,
    				char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
    {
    	char *varsubst, *field;
    	struct ast_str *str = ast_str_thread_get(&result_buf, 16);
    	int fieldindex = 0, res = 0;
    	AST_DECLARE_APP_ARGS(args,
    		AST_APP_ARG(varname);
    		AST_APP_ARG(delim);
    		AST_APP_ARG(field);
    	);
    	char delim[2] = "";
    	size_t delim_used;
    
    	if (!str) {
    		return -1;
    	}
    
    	AST_STANDARD_APP_ARGS(args, parse);
    
    	if (args.argc < 3) {
    		ast_log(LOG_ERROR, "Usage: FIELDNUM(<listname>,<delimiter>,<fieldvalue>)\n");
    		res = -1;
    	} else {
    
    		varsubst = ast_alloca(strlen(args.varname) + 4);
    
    		sprintf(varsubst, "${%s}", args.varname);
    
    		ast_str_substitute_variables(&str, 0, chan, varsubst);
    
    		if (ast_str_strlen(str) == 0 || ast_strlen_zero(args.delim)) {
    			fieldindex = 0;
    		} else if (ast_get_encoded_char(args.delim, delim, &delim_used) == -1) {
    			res = -1;
    		} else {
    			char *varval = ast_str_buffer(str);
    
    			while ((field = strsep(&varval, delim)) != NULL) {
    				fieldindex++;
    
    				if (!strcasecmp(field, args.field)) {
    					break;
    				}
    			}
    
    			if (!field) {
    				fieldindex = 0;
    			}
    
    			res = 0;
    		}
    	}
    
    	if (sbuf) {
    		ast_str_set(sbuf, len, "%d", fieldindex);
    	} else {
    		snprintf(buf, len, "%d", fieldindex);
    	}
    
    	return res;
    }
    
    static int function_fieldnum(struct ast_channel *chan, const char *cmd,
    			     char *parse, char *buf, size_t len)
    {
    	return function_fieldnum_helper(chan, cmd, parse, buf, NULL, len);
    }
    
    static int function_fieldnum_str(struct ast_channel *chan, const char *cmd,
    				 char *parse, struct ast_str **buf, ssize_t len)
    {
    	return function_fieldnum_helper(chan, cmd, parse, NULL, buf, len);
    }
    
    static struct ast_custom_function fieldnum_function = {
    	.name = "FIELDNUM",
    	.read = function_fieldnum,
    	.read2 = function_fieldnum_str,
    };
    
    
    static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **bufstr, ssize_t len)
    
    {
    	AST_DECLARE_APP_ARGS(args,
    		AST_APP_ARG(listname);
    		AST_APP_ARG(delimiter);
    		AST_APP_ARG(fieldvalue);
    	);
    
    	struct ast_str *orig_list = ast_str_thread_get(&tmp_buf, 16);
    
    	struct ast_str *result, **result_ptr = &result;
    
    	if (buf) {
    
    		if (!(result = ast_str_thread_get(&result_buf, 16))) {
    			return -1;
    		}
    
    	} else {
    		/* Place the result directly into the output buffer */
    		result_ptr = bufstr;
    	}
    
    
    	if (args.argc < 3) {
    		ast_log(LOG_ERROR, "Usage: LISTFILTER(<listname>,<delimiter>,<fieldvalue>)\n");
    		return -1;
    	}
    
    
    	varsubst = ast_alloca(strlen(args.listname) + 4);
    
    	/* If we don't lock the channel, the variable could disappear out from underneath us. */
    	if (chan) {
    		ast_channel_lock(chan);
    	}
    
    	ast_str_substitute_variables(&orig_list, 0, chan, varsubst);
    
    	if (!ast_str_strlen(orig_list)) {
    
    		if (chan) {
    			ast_channel_unlock(chan);
    		}
    		return -1;
    	}
    
    	/* If the string isn't there, just copy out the string and be done with it. */
    
    	if (!strstr(ast_str_buffer(orig_list), args.fieldvalue)) {
    
    		if (buf) {
    
    			ast_copy_string(buf, ast_str_buffer(orig_list), len);
    
    		} else {
    
    			ast_str_set(result_ptr, len, "%s", ast_str_buffer(orig_list));
    
    		if (chan) {
    			ast_channel_unlock(chan);
    		}
    		return 0;
    	}
    
    	dlen = strlen(args.delimiter);
    
    	delim = ast_alloca(dlen + 1);
    
    	ast_get_encoded_str(args.delimiter, delim, dlen + 1);
    
    	if ((dlen = strlen(delim)) == 0) {
    		delim = ",";
    		dlen = 1;
    	}
    
    	flen = strlen(args.fieldvalue);
    
    
    	ast_str_reset(*result_ptr);
    
    	if (len > -1) {
    
    		ast_str_make_space(result_ptr, len ? len : ast_str_strlen(orig_list) + 1);
    
    	next = strstr(begin, delim);
    
    	do {
    		/* Find next boundary */
    		if (next) {
    			cur = next;
    			next = strstr(cur + dlen, delim);
    		} else {
    			cur = strchr(begin + dlen, '\0');
    		}
    
    		if (flen == cur - begin && !strncmp(begin, args.fieldvalue, flen)) {
    			/* Skip field */
    			begin += flen + dlen;
    		} else {
    			/* Copy field to output */
    
    				ast_str_append(result_ptr, len, "%s", delim);
    
    			ast_str_append_substr(result_ptr, len, begin, cur - begin);
    
    			begin = cur + dlen;
    		}
    	} while (*cur != '\0');
    	if (chan) {
    		ast_channel_unlock(chan);
    	}
    
    
    	if (buf) {
    		ast_copy_string(buf, ast_str_buffer(result), len);
    	}
    
    static int listfilter_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
    {
    	return listfilter(chan, cmd, parse, buf, NULL, len);
    }
    
    static int listfilter_read2(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
    {
    	return listfilter(chan, cmd, parse, NULL, buf, len);
    }
    
    
    static struct ast_custom_function listfilter_function = {
    	.name = "LISTFILTER",
    
    	.read = listfilter_read,
    	.read2 = listfilter_read2,
    
    static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
    
    		  size_t len)
    
    	AST_DECLARE_APP_ARGS(args,
    
    			     AST_APP_ARG(allowed);
    			     AST_APP_ARG(string);
    
    	int32_t bitfield[8] = { 0, }; /* 256 bits */
    
    	AST_STANDARD_RAW_ARGS(args, parse);
    
    	if (!args.string) {
    
    		ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
    
    		return -1;
    
    	if (args.allowed[0] == '"' && !ast_opt_dont_warn) {
    		ast_log(LOG_WARNING, "FILTER allowed characters includes the quote (\") character.  This may not be what you want.\n");
    	}
    
    
    		if (ast_get_encoded_char(args.allowed, &c1, &consumed))
    
    			return -1;
    		args.allowed += consumed;
    
    		if (*(args.allowed) == '-') {
    
    			if (ast_get_encoded_char(args.allowed + 1, &c2, &consumed))
    
    			if ((unsigned char) c2 < (unsigned char) c1 && !ast_opt_dont_warn) {
    
    				ast_log(LOG_WARNING, "Range wrapping in FILTER(%s,%s).  This may not be what you want.\n", parse, args.string);
    			}
    
    
    			/*!\note
    			 * Looks a little strange, until you realize that we can overflow
    			 * the size of a char.
    			 */
    
    			for (ac = (unsigned char) c1; ac != (unsigned char) c2; ac++) {
    
    				bitfield[ac / 32] |= 1 << (ac % 32);
    			}
    
    			ast_debug(4, "c1=%d, consumed=%d, args.allowed=%s\n", c1, (int) consumed, args.allowed - consumed);
    
    		}
    	}
    
    	for (ac = 1; ac != 0; ac++) {
    		if (bitfield[ac / 32] & (1 << (ac % 32))) {
    			allowed[allowedlen++] = ac;
    		}
    
    	for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
    
    			*outbuf++ = *(args.string);
    
    static struct ast_custom_function filter_function = {
    
    static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
    {
    	AST_DECLARE_APP_ARGS(args,
    		AST_APP_ARG(varname);
    		AST_APP_ARG(find);
    		AST_APP_ARG(replace);
    	);
    	char *strptr, *varsubst;
    
    	RAII_VAR(struct ast_str *, str, ast_str_create(16), ast_free);
    
    	char find[256]; /* Only 256 characters possible */
    	char replace[2] = "";
    	size_t unused;
    
    	AST_STANDARD_APP_ARGS(args, data);
    
    	if (!str) {
    		return -1;
    	}
    
    	if (args.argc < 2) {
    		ast_log(LOG_ERROR, "Usage: %s(<varname>,<search-chars>[,<replace-char>])\n", cmd);
    		return -1;
    	}
    
    	/* Decode escapes */
    	ast_get_encoded_str(args.find, find, sizeof(find));
    	ast_get_encoded_char(args.replace, replace, &unused);
    
    	if (ast_strlen_zero(find) || ast_strlen_zero(args.varname)) {
    		ast_log(LOG_ERROR, "The characters to search for and the variable name must not be empty.\n");
    		return -1;
    	}
    
    
    	varsubst = ast_alloca(strlen(args.varname) + 4);
    
    	sprintf(varsubst, "${%s}", args.varname);
    	ast_str_substitute_variables(&str, 0, chan, varsubst);
    
    	if (!ast_str_strlen(str)) {
    		/* Blank, nothing to replace */
    		return -1;
    	}
    
    	ast_debug(3, "String to search: (%s)\n", ast_str_buffer(str));
    	ast_debug(3, "Characters to find: (%s)\n", find);
    	ast_debug(3, "Character to replace with: (%s)\n", replace);
    
    	for (strptr = ast_str_buffer(str); *strptr; strptr++) {
    		/* buf is already a mutable buffer, so we construct the result
    		 * directly there */
    		if (strchr(find, *strptr)) {
    			if (ast_strlen_zero(replace)) {
    
    				memmove(strptr, strptr + 1, strlen(strptr + 1) + 1);
    
    				strptr--;
    			} else {
    				/* Replace character */
    				*strptr = *replace;
    			}
    		}
    	}
    
    	ast_str_set(buf, len, "%s", ast_str_buffer(str));
    	return 0;
    }
    
    static struct ast_custom_function replace_function = {
    	.name = "REPLACE",
    	.read2 = replace,
    };
    
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    static int strreplace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
    {
    	char *varsubstr; /* substring for input var */
    
    	char *start; /* Starting pos of substring search. */
    	char *end; /* Ending pos of substring search. */
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    	int find_size; /* length of given find-string */
    
    	unsigned max_matches; /* number of matches we find before terminating search */
    	unsigned count; /* loop counter */
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    	struct ast_str *str = ast_str_thread_get(&result_buf, 16); /* Holds the data obtained from varname */
    
    	AST_DECLARE_APP_ARGS(args,
    		AST_APP_ARG(varname);
    		AST_APP_ARG(find_string);
    		AST_APP_ARG(replace_string);
    		AST_APP_ARG(max_replacements);
    
    		AST_APP_ARG(other);	/* Any remining unused arguments */
    
    	/* Guarantee output string is empty to start with. */
    	ast_str_reset(*buf);
    
    	if (!str) {
    		/* We failed to allocate str, forget it.  We failed. */
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    		return -1;
    	}
    
    
    	/* Parse the arguments. */
    	AST_STANDARD_APP_ARGS(args, data);
    
    	if (args.argc < 2) {
    		/* Didn't receive enough arguments to do anything */
    		ast_log(LOG_ERROR,
    			"Usage: %s(<varname>,<find-string>[,<replace-string>,[<max-replacements>]])\n",
    			cmd);
    
    Jonathan Rose's avatar
    Jonathan Rose committed
    		return -1;
    	}
    
    	/* No var name specified. Return failure, string is already empty. */
    	if (ast_strlen_zero(args.varname)) {
    		return -1;
    	}
    
    	/* Zero length find strings are a no-no. Kill the function if we run into one. */
    	if (ast_strlen_zero(args.find_string)) {