The FILTER() dialplan function is used to filter strings by only allowing characters that you have specified. This is a perfect candidate for controlling which characters you want to pass to the Dial() application, or any other application which will contain dynamic information passed to Asterisk from an external source. Lets take a look at how we can use FILTER() to control what
data we allow.
Using our previous example to accept any string length of 2 or more characters, starting with a number of zero through nine, we can use FILTER() to limit what we will accept to just numbers. Our example would then change to something like:
[incoming]
exten => _X.,1,Verbose(2,Incoming call to extension ${EXTEN})
exten => _X.,n,Dial(SIP/${FILTER(0-9,${EXTEN})})
exten => _X.,n,Hangup()
Note how we've wrapped the ${EXTEN} channel variable with the FILTER() function which will then only pass back characters that fit into the numerical range that we've defined.
Alternatively, if we didn't want to utilize the FILTER() function within the Dial() application directly, we could save the value to a channel variable, which has a side effect of being usable in other locations of your dialplan if necessary, and to handle error checking in a separate location.
[incoming]
exten => _X.,1,Verbose(2,Incoming call to extension ${EXTEN})
exten => _X.,n,Set(SAFE_EXTEN=${FILTER(0-9,${EXTEN})})
exten => _X.,n,Dial(SIP/${SAFE_EXTEN})
exten => _X.,n,Hangup()
Now we can use the ${SAFE_EXTEN} channel variable anywhere throughout the rest of our dialplan, knowing we've already filtered it. We could also perform an error check to verify that what we've received in ${EXTEN} also matches the data passed back by FILTER(), and to fail the call if things do not match.
[incoming]
exten => _X.,1,Verbose(2,Incoming call to extension ${EXTEN})
exten => _X.,n,Set(SAFE_EXTEN=${FILTER(0-9,${EXTEN})})
exten => _X.,n,GotoIf($[${EXTEN} != ${SAFE_EXTEN}]?error,1)
exten => _X.,n,Dial(SIP/${SAFE_EXTEN})
exten => _X.,n,Hangup()
exten => error,1,Verbose(2,Values of EXTEN and SAFE_EXTEN did not match.)
exten => error,n,Verbose(2,EXTEN: "${EXTEN}" -- SAFE_EXTEN: "${SAFE_EXTEN}")
exten => error,n,Playback(silence/1&invalid)
exten => error,n,Hangup()
Another example would be using FILTER() to control the characters we accept when we're expecting to get a SIP URI for dialing.
[incoming]
exten => _[0-9a-zA-Z].,1,Verbose(2,Incoming call to extension ${EXTEN})
exten => _[0-9a-zA-Z].,n,Dial(SIP/${FILTER(.@0-9a-zA-Z,${EXTEN})
exten => _[0-9a-zA-Z].,n,Hangup()
Of course the FILTER() function doesn't check the formatting of the incoming request. There is also the REGEX() dialplan function which can be used to determine if the string passed to it matches the regular expression you've created, and to take proper action on whether it matches or not. The creation of regular expressions is left as an exercise for the reader.
More information about the FILTER() and REGEX() dialplan functions can be found by typing "core show function FILTER" and "core show function REGEX" from your Asterisk console.