GNU Mailutils Manual (split by section):   Section:   Chapter:FastBack: Sieve Language   Up: Sieve Language   FastForward: Reporting Bugs   Contents: Table of ContentsIndex: Function Index

5.6 Tests

This section describes the built-in tests supported by GNU libmu_sieve. In the discussion below the following macro-notations are used:

match-type

This tag specifies the matching type to be used with the test. It can be one of the following:

:is

The :is match type describes an absolute match; if the contents of the first string are absolutely the same as the contents of the second string, they match. Only the string “frobnitzm” is the string “frobnitzm”. The null key “:is” and only “:is” the null value. This is the default match-type.

:contains

The :contains match type describes a substring match. If the value argument contains the key argument as a substring, the match is true. For instance, the string “frobnitzm” contains “frob” and “nit”, but not “fbm”. The null key “” is contained in all values.

:matches

The :matches version specifies a wildcard match using the characters ‘*’ and ‘?’. ‘*’ matches zero or more characters, and ‘?’ matches a single character. ‘?’ and ‘*’ may be escaped as ‘\\?’ and ‘\\*’ in strings to match against themselves. The first backslash escapes the second backslash; together, they escape the ‘*’.

:regex

The :regex version specifies a match using POSIX Extended Regular Expressions.

:value relation

The :value match type does a relational comparison between strings. Valid values for relation are:

"eq"

Equal

"ne"

Not Equal

"gt"

Greater Than

"ge"

Greater than or Equal

"lt"

Less Than

"le"

Less than or Equal

:count relation

This match type first determines the number of the specified entities (headers, addresses, etc.) in the message and does a relational comparison of the number of entities to the values specified in the test expression. The test expression must be a list of one element.

comparator

A comparator syntax item is defined as follows:

:comparator "comparator-name"

It instructs sieve to use the given comparator with the test. If comparator-name is not one of ‘i;octet’, ‘i;ascii-casemap’ it must be required prior to using it. For example:

require "comparator-i;ascii-numeric";

if header :comparator "i;ascii-numeric" :is "X-Num" "10"
  {
    ...
address-part

This syntax item is used when testing structured Internet addresses. It specifies which part of an address must be used in comparisons. Exactly one of the following tags may be used:

:all

Use the whole address. This is the default.

:localpart

Use local part of the address.

:domain

Use domain part of the address.

Notice, that match-type modifiers interact with comparators. Some comparators are not suitable for matching with :contains or :matches. If this occurs, sieve issues an appropriate error message. For example, the statement:

if header :matches :comparator "i;ascii-numeric"

would result in the following error message:

comparator `i;ascii-numeric' is incompatible with match type `:matches'
in call to `header'

GNU Sieve supports two kinds of tests. Built-in tests are defined within the library and do not require any external files. External tests are loadable modules that can be linked in at run time using the require statement (see Require Statement).

5.6.1 Built-in Tests

Test: false

This test always evaluates to “false”.

Test: true

This test always evaluates to “true”.

Test: address [address-part] [comparator] [match-type] header-names key-list

Tagged arguments:

address-part

Selects the address part to compare. Default is the whole email address (:all).

comparator

Specifies the comparator to be used instead of the default i;ascii-casemap.

match-type

Specifies the match type to be used instead of the default :is.

Required arguments:

header-names

A list of header names.

key-list

A list of address values.

The address test matches Internet addresses in structured headers that contain addresses. It returns true if any header contains any key in the specified part of the address, as modified by comparator and match-type optional arguments.

This test returns true if any combination of the header-names and key-list arguments match.

The address primitive never acts on the phrase part of an email address, nor on comments within that address. Use the header test instead. It also never acts on group names, although it does act on the addresses within the group construct.

Example:

if address :is :all "from" "tim@example.com"
  {
     discard;
  } 
Test: size [:over | :under] limit(number)

The size test deals with the size of a message. The required argument limit represents the size of the message in bytes. It may be suffixed with the following quantifiers:

k
K

The number is expressed in kilobytes.

m
M

The number is expressed in megabytes.

g
G

The number is expressed in gigabytes.

If the tagged argument is ‘:over’, and the size of the message is greater than number, the test is true; otherwise, it is false.

If the argument is ‘:under’, and the size of the message is less than the number, the test is true; otherwise, it is false.

Otherwise, the test is true only if the size of the message equals exactly number. This is a GNU extension.

The size of a message is defined to be the number of octets from the initial header until the last character in the message body.

Test: envelope [address-part] [comparator] [match-type] envelope-part(string-list) key-list(string-list)

Tagged arguments:

address-part

Selects the address part to compare. Default is the whole email address (:all).

comparator

Specifies the comparator to be used instead of the default i;ascii-casemap.

match-type

Specifies the match type to be used instead of the default :is.

Required arguments:

envelope-parts

A list of envelope parts to operate upon.

key-list

A list of address values.

The envelope test is true if the specified part of the SMTP envelope matches the specified key.

If the envelope-part strings is (case insensitive) ‘from’, then matching occurs against the FROM address used in the SMTP MAIL command.

Notice, that due to the limitations imposed by SMTP envelope structure the use of any other values in envelope-parts header is meaningless.

Test: exists header-names(string-list)

Required arguments:

header-names

List of message header names.


The exists test is true if the headers listed in header-names argument exist within the message. All of the headers must exist or the test is false.

The following example throws out mail that doesn’t have a From header and a Date header:

if not exists ["From","Date"]
  {
     discard;
  }
Test: header [comparator] [match-type] [:mime] header-names(string-list) key-list(string-list)

Tagged arguments:

comparator

Specifies the comparator to be used instead of the default i;ascii-casemap.

match-type

Specifies the match type to be used instead of the default :is.

:mime

This tag instructs header to search through the mime headers in multipart messages as well.


Required arguments:

header-names

A list of header names.

key-list

A list of header values.


The header test evaluates to true if any header name matches any key. The type of match is specified by the optional match argument, which defaults to ":is" if not explicitly given.

The test returns true if any combination of the header-names and key-list arguments match.

If a header listed in header-names exists, it contains the null key (‘""’). However, if the named header is not present, it does not contain the null key. So if a message contained the header

X-Caffeine: C8H10N4O2

these tests on that header evaluate as follows:

header :is ["X-Caffeine"] [""] ⇒ false
header :contains ["X-Caffeine"] [""] ⇒ true

5.6.2 External Tests

Test: numaddr [:over | :under] header-names(string-list) count(number)


Synopsis:

require "test-numaddr";
…
if numaddr args
  {
    …
  }

Description: This test is provided as an example of loadable extension tests. You must use ‘require "test-numaddr"’ statement before actually using it.

The numaddr test counts Internet addresses in structured headers that contain addresses. It returns true if the total number of addresses satisfies the requested relation.

If the tagged argument is ‘:over’ and the number of addresses is greater than count, the test is true; otherwise, it is false.

If the tagged argument is ‘:under’ and the number of addresses is less than count, the test is true; otherwise, it is false.

If the tagged argument is not given, ‘:over’ is assumed.

Test: pipe [:envelope] [:header] [:body] [:exit code(number)] [:signal code(number)] command(string)

Synopsis:

require "test-pipe";

if pipe command
  {
    …
  }

Description: The pipe test executes a shell command specified by its argument and pipes the entire message (including envelope) to its standard input. When given, tags :envelope, :header, and :body control what parts of the message to pipe to the command.

In the absence of the :exit tag, the test returns true if the command exits with code 0. If :exit is given, the test returns true if the command exits with code equal to its argument.

The :signal tag determines the result of the test in case if the program exits on signal. By default, the test returns false. If :signal is given and the number of signal which caused the program to terminate matches its argument, the test returns true.

Test: spamd [:host tcp-host(string)] [:port tcp-port(number)] [:socket unix-socket(string)] [:user name(string)] [:over | :under limit(string)]

Synopsis:

require "test-spamd";
…
if spamd args
  {
    # This is spam
    …
  }

Description: This test is an interface to SpamAssassin filter. It connects to the spamd daemon using connection parameters specified by tagged arguments :host and :port (if the daemon is listening on an INET socket), or :socket (if the daemon is listening on a UNIX socket) and returns true, if SpamAssassin qualifies the message as spam. Tagged argument limit alters the default behavior. Its value is a string representation of a floating point number. If the tag :over is used, then the test returns true if the spam score returned from SpamAssassin is greater than limit. Otherwise, if :under is used, the test returns true if the spam score is less than limit. The comparison takes into account three decimal digits.

Tagged argument :user allows to select a specific user profile. If it is not given, the user name is determined using the effective UID.

Before returning, the spamd test adds the following headers to the message:

X-Spamd-Status

YES’ or ‘NO’, depending on whether the message is qualified as spam or ham.

X-Spamd-Score

Actual spam score value.

X-Spamd-Threshold

Spam score threshold, as configured in SpamAssassin settings.

X-Spamd-Keywords

Comma-separated list of keywords, describing the spam checks that succeeded for this message.

Example:

request "test-spamd";

if spamd :host 127.0.0.1 :port 3333
  {
     discard;
  }
Test: list [comparator] [match-type] [ :delim delimiters(string) ] headers(string-list) keys(string-list)

Synopsis:

require "test-list";
if list args
  {
     … 
  }

Description: The list test evaluates to true if any of headers matches any key from keys. Each header is regarded as containing a list of keywords. By default, comma is assumed as list separator. This can be overridden by specifying the :delim tag, whose value is a string consisting of valid list delimiter characters.

Example:

This test can be used in conjunction with the spamd test described above:

require ["fileinto", "test-spamd", "test-list"];

if spamd :host 127.0.0.1 :port 3333
  {
     if list :matches :delim " ,"
             "X-Spamd-Keywords" [ "HTML_*", "FORGED_*" ]
       {      
          fileinto "~/mail/spam";
       }
     else
       {
          discard;
       }
  }
Test: timestamp [:before | :after] header(string) date(string)

Synopsis:

require "test-timestamp";

if timestamp arg
  {
     …
  }

Description: The timestamp test compares the value of a structured date header field (header) with the given date (date).

If the tagged argument is :after and the date from the header is after the specified date the result is true, otherwise, if the header date is before the given date, the result is false.

If the tagged argument is :before and the date from the header is before the specified date the result is true, otherwise, if the header date is after the given date, the result is false.

If no tagged argument is supplied, :after is assumed.

Almost any date format is understood. See Date Input Formats, for a detailed information on date formats.

Example:

The test below succeeds if the date in ‘X-Expire-Timestamp’ header is more than 5 days older than the current date:

require "test-timestamp";

if timestamp :before "X-Expire-Timestamp" "now - 5 days"
  {
     discard;
  }

GNU Mailutils Manual (split by section):   Section:   Chapter:FastBack: Sieve Language   Up: Tests   FastForward: Reporting Bugs   Contents: Table of ContentsIndex: Function Index