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