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