| 1 | #! PERL_COMMAND -w |
| 2 | # $Cambridge: exim/src/src/transport-filter.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ |
| 3 | |
| 4 | # This is a Perl script to demonstrate the possibilities of on-the-fly |
| 5 | # delivery filtering in Exim. It is presented with a message on its standard |
| 6 | # input, and must copy it to the standard output, transforming it as it |
| 7 | # pleases, but of course it must keep to the syntax of RFC 822 for the headers. |
| 8 | |
| 9 | # The filter is run before any SMTP-specific processing, such as turning |
| 10 | # \n into \r\n and escaping lines beginning with a dot. |
| 11 | # |
| 12 | # Philip Hazel, May 1997 |
| 13 | ############################################################################# |
| 14 | |
| 15 | |
| 16 | # If the filter is called with any arguments, insert them into the message |
| 17 | # as X-Arg headers, just to verify what they are. |
| 18 | |
| 19 | for ($ac = 0; $ac < @ARGV; $ac++) |
| 20 | { |
| 21 | printf("X-Arg%d: %s\n", $ac, $ARGV[$ac]); |
| 22 | } |
| 23 | |
| 24 | # Now read the header portion of the message; this is easy to do in Perl |
| 25 | |
| 26 | $/ = ""; # set paragraph mode |
| 27 | chomp($headers = <STDIN>); # read a paragraph, remove trailing newlines |
| 28 | $/ = "\n"; # unset paragraph mode |
| 29 | |
| 30 | # Splitting up a sequence of unique headers is easy to do in Perl, but a |
| 31 | # message may contain duplicate headers of various kinds. It is better |
| 32 | # to extract the headers one wants from the whole paragraph, do any appropriate |
| 33 | # munging, and then put them back (unless removing them altogether). Messing |
| 34 | # with "Received:" headers is not in any case to be encouraged. |
| 35 | |
| 36 | # As a demonstration, we extract the "From:" header, add a textual comment |
| 37 | # to it, and put it back. |
| 38 | |
| 39 | ($pre, $from, $post) = |
| 40 | $headers =~ /^(|(?:.|\n)+\n) (?# Stuff preceding the From header, |
| 41 | which is either null, or any number |
| 42 | of characters, including \n, ending |
| 43 | with \n.) |
| 44 | From:[\s\t]* (?# Header name, with optional space or tab.) |
| 45 | ((?:.|\n)*?) (?# Header body, which contains any chars, |
| 46 | including \n, but we want to make it as |
| 47 | short as possible so as not to include |
| 48 | following headers by mistake.) |
| 49 | (|\n\S(?:.|\n)*)$ (?# Header terminates at end or at \n followed |
| 50 | by a non-whitespace character and |
| 51 | remaining headers.) |
| 52 | /ix; # case independent, regular expression, |
| 53 | # use extended features (ignore whitespace) |
| 54 | |
| 55 | # Only do something if there was a From: header, of course. It has been |
| 56 | # extracted without the final \n, which is on the front of the $post |
| 57 | # variable. |
| 58 | |
| 59 | if ($pre) |
| 60 | { |
| 61 | $headers = $pre . "From: $from (this is an added comment)" . $post; |
| 62 | } |
| 63 | |
| 64 | # Add a new header to the end of the headers; remember that the final |
| 65 | # \n isn't there. |
| 66 | |
| 67 | $headers .= "\nX-Comment: Message munged"; |
| 68 | |
| 69 | # Write out the processed headers, plus a blank line to separate them from |
| 70 | # the body. |
| 71 | |
| 72 | printf(STDOUT "%s\n\n", $headers); |
| 73 | |
| 74 | # As a demonstration of munging the body of a message, reverse all the |
| 75 | # characters in each line. |
| 76 | |
| 77 | while (<STDIN>) |
| 78 | { |
| 79 | chomp; |
| 80 | $_ = reverse($_); |
| 81 | printf(STDOUT "%s\n", $_); |
| 82 | } |
| 83 | |
| 84 | # End |