Repelling the Invaders Part 3
- 0
- Add a Comment
Repelling the Invaders - Part 3
This week we’ve been talking about email filtering in Linux using procmail. Today, we’ll begin looking at the .procmailrc file, the heart and soul of this sophisticated email filtering system.
The .procmailrc file, for the purposes of our discussion, resides in the user’s home directory, or ~/.procmailrc. The file itself is a combination of recipes and environment variables that procmail will use to filter, forward or autrorespond to your incoming email. We looked at a few simple recipes as examples of regular expressions yesterday, but let’s look a bit deeper today.
The convention for recipes in .procmailrc is as follows:
:0 [flags] [ : [locallockfile] ]
[zero or more conditions (one per line)]
[exactly one action line]
There really are only two types of procmail recipes: delivering and non-delivering. When a delivering recipe finds a match in the conditions, it stops processing the .procmailrc file after executing the action specified for that recipe. Delivery, in this case, actually means that the header and/or body of the incoming email will be written to a file, absorbed by another program or forwarded to an mail address.
Non-delivering recipes simply cause the output of a program or filter to be captured back by procmail. Recipes that start a nesting block are also considered non-delivering. You’ll see examples of both of these types of non-delivering recipes over the next few days.
The [locallockfile] is necessary when, for example, several procmail programs run concurrently. The locallockfile locks down a file while it’s being written or processed by procmail, then unlocks it when that particular instance of procmail is finished.
We looked yesterday at the regular expression set for procmail. Let’s take a look today at the [flags] section of the recipe. While regular expressions tell procmail what to look for, flags tell the program the initial actions to perform, or the conditions to evaluate prior to processing the regular expressions. Here’s a list of the flags available to procmail:
| Flag | Action |
| H | Egrep the header (default) |
| B | Egrep the body |
| D | Distinguish between upper and lower case |
| A | Makes the current recipe dependent on the preceding recipe in a nested block |
| a | Has the same meaning as ‘A’ but requires the successful completion of the preceding recipe |
| E | Execute this recipe only if the immediately preceding recipe was not executed |
| e | Execute this recipe only if the immediately preceding recipe was executed but failed. |
| h | Redirect the header via the pipe (default) |
| b | Redirect the body via the pipe |
| c | Generate a carbon copy of this mail |
A quick note to supplement the list of flags. Egrep is a more powerful extension of the grep program we talked about early in Penguin Shell. It’s sole purpose in life is to find patterns of characters and report back. And, if you’re not sure about the pipe, we’ll talk a bit more about redirection and the use of pipes over the next few days.
It’s not an easy road, huh? That’s okay. If you’re willing to hang in, there’s plenty of useful Linux knowledge to be gained as we move through procmail. Tomorrow, we’ll look at some useful recipes and talk a bit about environment variables.
