#### 5.8.3 The variables extension ¶

The ‘`variables`’ extension is defined in RFC 5229. It is a built-in extension. It introduces support for variables in Sieve scripts.

There are two kind of variables: user-defined and match variables.

A user-defined variable is initialized using the `set` action:

Action: set `[modifiers] name(string) value(string)` ¶

  Stores the specified value in the variable identified by name. Optional modifiers are applied on value before it is stored in the variable.

  The following modifiers are available:

  `:lower`

    Convert value to lower case letters.

  `:upper`

    Convert value to upper case letters.

  `:lowerfirst`

    Convert the first character in value to lower case.

  `:upperfirst`

    Convert the first character in value to upper case.

  `:quotewildcard`

    Quote wildcard characters (‘`*`’, ‘`?`’, ‘`\`’) by prefixing each occurrence with a backslash (‘`\`’). This can be used to ensure that the variable will only match a literal occurrence if used as a parameter to `:matches`.

  `:length`

    The value is the decimal number of characters in the expansion, converted to a string.

  When several modifiers are present, they are applied in the following order of precedence (largest value first):

  precedence | modifiers
  --- | ---
  40 | `:lower` or `:upper`
  30 | `:lowerfirst` or `:upperfirst`
  20 | `:quotewildcard`
  10 | `:length`

  Modifiers having the same precedence (i.e. listed on the same row in the above table) cannot be used together.

Variables are referenced within text strings using the construct ‘`${name}`’, where name is the name of the variable as it appeared in the first parameter to the `set` statement. For example:

    require "variables";

    set "sender" "root
    ":

    if envelope :matches "${sender}"
    {
       ...
    }

Match variables refer to parts of the most recently evaluated successful match of type `:matches` or `:regex`. They have names consisting entirely of decimal digits. The variable ‘`${0}`’ refers to the entire matched expression. The variable ‘`${1}`’ refers to the substring matching the first occurrence of the wildcard (‘`?`’ and ‘`*`’), ‘`${2}`’ refers to the second occurrence and so on. The wildcards match as little as possible (non-greedy matching). For example:

    require ["variables", "fileinto"];

    if header :matches "List-ID" "*<*
    " {
       fileinto "INBOX.lists.${2}";
       stop;
    }

If `:regex` match is used, the match variables starting from ‘`${1}`’ refer to the substrings of the argument value matching subsequent parenthesized groups of the regular expression.

Test: string `[comparator] [match-type] source(string-list) keys(string-list)` ¶

  The `string` test compares two strings according to the selected comparator and match type. The test evaluates to ‘`true`’ if any two strings from source and keys match.

  The ‘`:count`’ match used in ‘`string`’ counts each empty string as 0, and each non-empty one as 1. The count of a string list is the sum of the counts of the member strings.