Exim Filter Specification - Expansion items

Go to the first, previous, next, last section, table of contents.

## Expansion items

The following items are recognized in expanded strings. White space may be used between sub-items that are keywords or substrings enclosed in braces inside an outer set of braces, to improve readability. Within braces, however, white space is significant.

    ______________________________________________________________

    $<variable name> or ${<variable name>}

Substitute the contents of the named variable, for example

    $local_part
    ${domain}

The second form can be used to separate the name from subsequent alphanumeric characters. This form (using curly brackets) is available only for variables; it does not apply to message headers. The names of the variables are given in section 1.33 below. If the name of a non-existent variable is given, the expansion fails.

    ______________________________________________________________

    $header_<header name>: or $h_<header name>:

Substitute the contents of the named message header line, for example

    $header_reply-to:

The header names follow the syntax of RFC 822, which states that they may contain any printing characters except space and colon. Consequently, curly brackets do not terminate header names, and should not be used to enclose them as if they were variables. Attempting to do so causes a syntax error.

Upper-case and lower-case letters are synonymous in header names. If the following character is white space, the terminating colon may be omitted, but this is not recommended, because you may then forget it when it is needed. When white space terminates the header name, it is included in the expanded string. If the message does not contain the given header, the expansion item is replaced by an empty string. (See the def condition in section 1.32 for a means of testing for the existence of a header.) If there is more than one header with the same name, they are all concatenated to form the substitution string, with a newline character between each of them. However, if the length of this string exceeds 64K, any further headers of the same name are ignored.

    ______________________________________________________________

    ${<op>:<string>}

The string is first itself expanded, and then the operation specified by <op> is applied to it. For example,

    ${lc:$local_part}

A list of operators is given in section 1.31 below. The string starts with the first character after the colon, which may be leading white space.

    ______________________________________________________________

    ${extract{<key>}{<string1>}{<string2>}{<string3>}}

The key and <string1> are first expanded separately. The key must not consist entirely of digits. For the string, the result must be of the form:

    <key1> = <value1>  <key2> = <value2> ...

where the equals signs and spaces are optional. If any of the values contain white space, they must be enclosed in double quotes, and any values that are enclosed in double quotes are subject to escape processing as described in section 5. The expanded <string1> is searched for the value that corresponds to the key. If it is found, <string2> is expanded, and replaces the whole item; otherwise <string3> is used. During the expansion of <string2> the variable $value contains the value that has been extracted. Afterwards, it is restored to any previous value it might have had.

If {<string3>} is omitted, the item is replaced by nothing if the key is not found. If {<string2>} is also omitted, the value that was looked up is used. Thus, for example, these two expansions are identical, and yield `2001':

    ${extract{gid}{uid=1984 gid=2001}}
    ${extract{gid}{uid=1984 gid=2001}{$value}}

Instead of {<string3>} the word `fail' (not in curly brackets) can appear, for example:

    ${extract{Z}{A=... B=...}{$value} fail }

{<string2>} must be present for `fail' to be recognized. When this syntax is used, if the extraction fails, the entire expansion fails. This causes processing of the filter file to fail, which either delays delivery of the message, or causes the filter file to be ignored, depending on how the administrator has configured Exim.

    ______________________________________________________________

    ${extract{<number>}{<separators>}{<string1>}{<string2>}{<string3>}}

The <number> argument must consist entirely of decimal digits. This is what distinguishes this form of extract from the previous kind. It behaves in the same way, except that, instead of extracting a named field, it extracts from <string1> the field whose number is given as the first argument. The first field is numbered one. If the number is greater than the number of fields in the string, the result is empty; if it is zero, the entire string is returned. The fields in the string are separated by any one of the characters in the separator string. For example:

    ${extract{2}{:}{x:42:99:& Mailer::/bin/bash}}

yields `42'. Two successive separators mean that the field between them is empty (for example, the sixth field above).

    ______________________________________________________________

    ${if <condition> {<string1>}{<string2>}}

If <condition> is true, <string1> is expanded and replaces the whole item; otherwise <string2> is used. For example,

    ${if eq {$local_part}{postmaster} {yes}{no} }

The second string need not be present; if it is not and the condition is not true, the item is replaced with nothing. Alternatively, the word `fail' may be present instead of the second string (without any curly brackets). In this case, the expansion is forced to fail if the condition is not true. The available conditions are described in section 1.32 below.

    ______________________________________________________________

    ${lookup{<key>} <search type> {<file>} {<string1>} {<string2>}}

    ${lookup <search type> {<query>} {<string1>} {<string2>}}

These items specify data lookups in files and databases, as discussed in chapter . The first form is used for single-key lookups, and the second is used for query-style lookups. The <key>, <file>, and <query> strings are expanded before use.

If there is any white space in a lookup item which is part of a filter command, the lookup item must be enclosed in double quotes. The use of data lookups in users' filter files may be locked out by the system administrator.

If the lookup succeeds, <string1> is expanded and replaces the entire item. During its expansion, the variable $value contains the data returned by the lookup. Afterwards it reverts to the value it had previously (at the outer level it is empty). If the lookup fails, <string2> is expanded and replaces the entire item. If {<string2>} is omitted, the replacement is null on failure. Alternatively, <string2> can itself be a nested lookup, thus providing a mechanism for looking up a default value when the original lookup fails.

If a nested lookup is used as part of <string1>, $value contains the data for the outer lookup while the parameters of the second lookup are expanded, and also while <string2> of the second lookup is expanded, should the second lookup fail.

Instead of {<string2>} the word `fail' can appear, and in this case, if the lookup fails, the entire expansion is forced to fail. If both {<string1>} and {<string2>} are omitted, the result is the looked up value in the case of a successful lookup, and nothing in the case of failure.

For single-key lookups, the string `partial-' is permitted to precede the search type in order to do partial matching, and * or *@ may follow a search type to request default lookups if the key does not match (see sections and ).

If a partial search is used, the variables $1 and $2 contain the wild and non-wild parts of the key during the expansion of the replacement text. They return to their previous values at the end of the lookup item.

This example looks up the postmaster alias in the conventional alias file.

    ${lookup {postmaster} lsearch {/etc/aliases} {$value}}

This example uses NIS+ to look up the full name of the user corresponding to the local part of an address, forcing the expansion to fail if it is not found.

    "${lookup nisplus {[name=$local_part],passwd.org_dir:gcos} \
      {$value}fail}"

    ______________________________________________________________

    ${lookup{<key:subkey>} <search type> {<file>} {<string1>} {<string2>}}

This is just a syntactic variation for a single-key lookup, surrounded by an extract item. It searches for <key> in the file as described above for single-key lookups; if it succeeds, it extracts from the data a subfield which is identified by the <subkey>. For example, if a line in a linearly searched file contains

    alice: uid=1984 gid=2001

then expanding the string

    ${lookup{alice:uid}lsearch{<file name>}{$value}}

yields the string `1984'. If the subkey is not found in the looked up data, then <string2>, if present, is expanded and replaces the entire item. Otherwise the replacement is null. The example above could equally well be written like this:

    ${extract{uid}{${lookup{alice}lsearch{<file name>}}}}

and this is recommended, because this approach can also be used with query-style lookups.

    ______________________________________________________________

    ${sg{<subject>}{<regex>}{<replacement>}}

This item works like Perl's substitution operator (s) with the global (/g) option; hence its name. It takes three arguments: the subject string, a regular expression, and a substitution string. For example

    ${sg{abcdefabcdef}{abc}{xyz}}

yields `xyzdefxyzdef'. Because all three arguments are expanded before use, if any $ or \ characters are required in the regular expression or in the substitution string, they have to be escaped. For example

    ${sg {abcdef}{^(...)(...)\$}{\$2\$1}}

yields `defabc', and

    ${sg{1=A 4=D 3=C}{(\\d+)=}{K\$1=}}

yields `K1=A K4=D K3=C'.

    ______________________________________________________________

    ${tr{<subject>}{<characters>}{<replacements>}}

This item does single-character translation on its subject string. The second argument is a list of characters to be translated in the subject string. Each matching character is replaced by the corresponding character from the replacement list. For example

    ${tr{abcdea}{ac}{13}}

yields `1b3de1'. If there are duplicates in the second character string, the last occurrence is used. If the third string is shorter than the second, its last character is replicated. However, if it is empty, no translation takes place.

Go to the first, previous, next, last section, table of contents.