| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
$VERSION = '1.00'; |
| 4 |
|
| 5 |
use strict; |
| 6 |
use FileHandle; |
| 7 |
|
| 8 |
#------------------------------------------------------------------------------- |
| 9 |
|
| 10 |
my $LINE = 0; |
| 11 |
my $FILE_HANDLE = undef; |
| 12 |
my $START = 0; |
| 13 |
my $END = 0; |
| 14 |
my $READ_BUFFER = ''; |
| 15 |
|
| 16 |
sub reset_file |
| 17 |
{ |
| 18 |
my $file_handle = shift; |
| 19 |
|
| 20 |
$FILE_HANDLE = $file_handle; |
| 21 |
$LINE = 1; |
| 22 |
$START = 0; |
| 23 |
$END = 0; |
| 24 |
$READ_BUFFER = ''; |
| 25 |
} |
| 26 |
|
| 27 |
#------------------------------------------------------------------------------- |
| 28 |
|
| 29 |
# Need this for a lookahead. |
| 30 |
my $READ_CHUNK_SIZE = 0; |
| 31 |
|
| 32 |
sub read_email |
| 33 |
{ |
| 34 |
# Undefined read buffer means we hit eof on the last read. |
| 35 |
return 0 unless defined $READ_BUFFER; |
| 36 |
|
| 37 |
my $line = $LINE; |
| 38 |
|
| 39 |
$START = $END; |
| 40 |
|
| 41 |
# Look for the start of the next email |
| 42 |
LOOK_FOR_NEXT_HEADER: |
| 43 |
while($READ_BUFFER =~ m/^(From\s.*\d:\d+:\d.* \d{4})/mg) |
| 44 |
{ |
| 45 |
$END = pos($READ_BUFFER) - length($1); |
| 46 |
|
| 47 |
# Don't stop on email header for the first email in the buffer |
| 48 |
next if $END == 0; |
| 49 |
|
| 50 |
# Keep looking if the header we found is part of a "Begin Included |
| 51 |
# Message". |
| 52 |
my $end_of_string = substr($READ_BUFFER, $END-200, 200); |
| 53 |
if ($end_of_string =~ |
| 54 |
/\n-----( Begin Included Message |Original Message)-----\n[^\n]*\n*$/i) |
| 55 |
{ |
| 56 |
next; |
| 57 |
} |
| 58 |
|
| 59 |
# Found the next email! |
| 60 |
my $email = substr($READ_BUFFER, $START, $END-$START); |
| 61 |
$LINE += ($email =~ tr/\n//); |
| 62 |
|
| 63 |
return (1, $email, $line); |
| 64 |
} |
| 65 |
|
| 66 |
# Didn't find next email in current buffer. Most likely we need to read some |
| 67 |
# more of the mailbox. Shift the current email to the front of the buffer |
| 68 |
# unless we've already done so. |
| 69 |
$READ_BUFFER = substr($READ_BUFFER,$START) unless $START == 0; |
| 70 |
$START = 0; |
| 71 |
|
| 72 |
# Start looking at the end of the buffer, but back up some in case the edge |
| 73 |
# of the newly read buffer contains the start of a new header. I believe the |
| 74 |
# RFC says header lines can be at most 90 characters long. |
| 75 |
my $search_position = length($READ_BUFFER) - 90; |
| 76 |
$search_position = 0 if $search_position < 0; |
| 77 |
|
| 78 |
# Can't use sysread because it doesn't work with ungetc |
| 79 |
if ($READ_CHUNK_SIZE == 0) |
| 80 |
{ |
| 81 |
local $/ = undef; |
| 82 |
|
| 83 |
if (eof $FILE_HANDLE) |
| 84 |
{ |
| 85 |
my $email = $READ_BUFFER; |
| 86 |
undef $READ_BUFFER; |
| 87 |
return (1, $email, $line); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
$READ_BUFFER = <$FILE_HANDLE>; |
| 92 |
pos($READ_BUFFER) = $search_position; |
| 93 |
goto LOOK_FOR_NEXT_HEADER; |
| 94 |
} |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
if (read($FILE_HANDLE, $READ_BUFFER, $READ_CHUNK_SIZE, length($READ_BUFFER))) |
| 99 |
{ |
| 100 |
pos($READ_BUFFER) = $search_position; |
| 101 |
goto LOOK_FOR_NEXT_HEADER; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
my $email = $READ_BUFFER; |
| 106 |
undef $READ_BUFFER; |
| 107 |
return (1, $email, $line); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sub Read_Chunk_Of_Body |
| 113 |
{ |
| 114 |
my $email = shift; |
| 115 |
|
| 116 |
local $/ = "\nFrom "; |
| 117 |
my $chunk = <$FILE_HANDLE>; |
| 118 |
local $/ = "From "; |
| 119 |
chomp $chunk; |
| 120 |
|
| 121 |
$LINE += ($chunk =~ tr/\n//); |
| 122 |
|
| 123 |
$$email .= $chunk; |
| 124 |
} |
| 125 |
|
| 126 |
die unless @ARGV; |
| 127 |
|
| 128 |
$FILE_HANDLE = new FileHandle($ARGV[0]); |
| 129 |
|
| 130 |
while(1) |
| 131 |
{ |
| 132 |
my ($status,$email,$line) = read_email(); |
| 133 |
exit unless $status; |
| 134 |
|
| 135 |
my ($header,$body) = $email =~ /(.*?\n\n)(.*)/s; |
| 136 |
|
| 137 |
|
| 138 |
$body =~ s/\w/X/g; |
| 139 |
|
| 140 |
|
| 141 |
{ |
| 142 |
my ($header_to) = $header =~ /^To: (.*)$/m; |
| 143 |
my ($header_subject) = $header =~ /^Subject: (.*)$/m; |
| 144 |
|
| 145 |
if (defined $header_to) |
| 146 |
{ |
| 147 |
my $modified_header_to = $header_to; |
| 148 |
$modified_header_to =~ s/\w/X/g; |
| 149 |
|
| 150 |
$header =~ s/To: \Q$header_to\E/To: $modified_header_to/g; |
| 151 |
} |
| 152 |
|
| 153 |
if (defined $header_subject) |
| 154 |
{ |
| 155 |
my $modified_header_subject = $header_subject; |
| 156 |
$modified_header_subject =~ s/\w/X/g; |
| 157 |
|
| 158 |
$header =~ s/Subject: \Q$header_subject\E/Subject: $modified_header_subject/g; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
print $header,$body; |
| 164 |
} |