[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 Conditional Statements

A conditional statement defines the control flow in a section. It allows to execute arbitrary actions depending on whether a certain condition is met. The conditional statement in its simplest form is:

 
if condition
  action-list-1
fi

If condition evaluates to true, then the list of statements action-list-1 is executed.

A simple condition has the following syntax:

 
part [sep] [op] [pattern-match-flags] regex

(where the square brackets denote optional parts). Its parts are:

part

The part specifies which part of the input should be considered when evaluating the condition. It is either ‘command’, meaning the text of an smtp command issued while sending the message, or ‘header’, meaning the value of an RFC822 header. Either of the two may be followed by the name of the corresponding command or header enclosed in square brackets. If this part is missing, all command or headers will be searched.

sep

Optional concatenation separator. See section Concatenations, for its meaning.

op

Either ‘=’, meaning “match”, or ‘!=’, meaning “does not match”. Missing op is equivalent to ‘=’.

pattern-match-flags

Optional pattern-match-flags alter the pattern matching type used in subsequent conditional expression. It will be described in detail in the section Regular Expressions.

regex

The regex is a regular expression enclosed in double quotes.

The condition yields true if the requested part of the input matches the given regular expression (if op is ‘=’), or does not match it (if op is ‘!=’).

The simplest example:

 
if header [Subject] "^ *Re:"
  ...
fi

The actions represented by … will be executed only if the ‘Subject:’ header of the message starts with ‘Re:’ optionally preceded by any amount of whitespace.

The more elaborate form of a conditional allows you to choose among the two different action sets depending on a given condition. The syntax is:

 
if condition
  action-list-1
else
  action-list-2
fi

Here, the action-list-1 is executed if the condition is met. Otherwise, action-list-2 is executed.

 
if condition
  action-list-1
else
  action-list-2  
fi

Note also, that in the examples above any of the statements action-list may contain conditionals, so that the conditional statements may be nested. This allows to create very sophisticated rule sets. As an example, consider the following statement:

 
if [List-Id] :re ".*<anubis-commit@gnu.org>"
  modify [Subject] "[Anubis Commit Notice] &"
else
  if [List-Id] :re ".*<bug-anubis@gnu.org>"
    modify [Subject] "[Anubis Bug Notice] &"
  else
    add [X-Passed] "Subject checking"
  fi
fi  

This statement, depending on the value of List-Id header, will prepend the Subject header with an identification string, or add an X-Passed header if no known List-Id was found.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Sergey Poznyakoff on December, 20 2008 using texi2html 1.78.