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

6.2 Conditional Statements

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

 
if part [pattern-match-flags] cond-expr
  action-list-1
fi
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.

The optional pattern-match-flags alter the pattern matching type used in subsequent conditional expression. It will be described in detail in the section 6.5 Regular Expressions. The cond-expr is a conditional expression. It consists of a series of conditions joined together with boolean operators `and' or `or' (see section 6.4 Boolean Operators). Each condition is:

= regexp
Returns true if the requested part of the input matches the given regular expression (regexp).

!= regexp
Returns true if the requested part of the input does not match the given regular expression.

not condition
Reverses the sense of condition

( cond-expr )
Returns the result of the conditional expression in parentheses. This is useful for changing operator precedence.

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 part [flags] cond-expr
  action-list-1
else
  action-list-2
fi

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

 
if part [flags] cond-expr
  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 ".*"
  modify [Subject] "[Anubis Commit Notice] &"
else
  if [List-Id] :re ".*"
    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 Wojciech Polak on December, 18 2004 using texi2html