2 # $Id: t-prot,v 1.312 2010/10/31 11:18:15 jochen Exp $
6 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
7 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
8 use constant VER => '2.101';
9 use constant REV => '';
10 use constant REL => q$Revision: 1.312 $=~m/(\d+(?:\.\d+)+)/;
11 # MTA expecting mail on STDIN
12 # (you might have to adjust this if using a different MTA)
13 use constant SENDMAIL => '/usr/sbin/sendmail -oi';
15 # (you might have to adjust those if your libc wants different values)
16 use constant EX_OK => 0;
17 use constant EX_USAGE => 64;
18 use constant EX_DATAERR => 65;
19 use constant EX_UNAVAILABLE => 69;
20 use constant EX_SOFTWARE => 70;
21 use constant EX_IOERR => 74;
22 use constant EX_BOUNCE => EX_UNAVAILABLE;
24 $ad $ads $bigqn $bigqx $boun $check $check_ratio $cr $crshrink $diff $elli
25 $fixind $footers $ftr_ad $ftr_ml $hdrs $indent $kamm $kdiff $kminl $kmaxl
26 $lax $lsig $maxsig $maxlines $mda $ml $gw $ms $ms_smart $msg_quote
27 $msg_ratio $mua $nohdr $ofile $pgpshort $pgpmove $pgpmovevrf $reply
28 $sani $sig $sigint $sign $spass $spass_prefix $sysl $trad $trsp
30 $gpg_WARNING $gpg_Warning $gpg_Cantcheck $gpg_aka $gpg_bad
31 $gpg_expired $gpg_good $gpg_bug
33 $mutt_attachment $mutt_contenttype $mutt_pgpsigned $mutt_beginsigned
34 $mutt_pgpclearsigned $mutt_pgpclearsigstart $mutt_pgpencrypted
35 $mutt_pgpoutstart $mutt_pgpoutend $mutt_pgpcryptend $mutt_pgpcryptstart
39 # Please adjust these vals to your needs only if you know what you are
40 # doing. They are no constants because command line can change them or
41 # they are used in some rexexp.
43 $maxsig = 4; # max. valid signature length
44 $maxlines = undef; # no limit of message lines
45 $crshrink = 2; # multiple blank lines are shrunk to $crshrink lines
46 $indent = '>'; # Indent string to identify a quoted line
47 $kminl = 65; # see decomb() for details
50 $pgpshort = 0; # hide pgp key ids if set
51 $pgpmove = 0; # move pgp output to bottom if set
53 $sign = 1; # max number of sigs tolerated, undef for no limit
54 $boun = "Blocked by $0: This user does not accept TOFUed email. Please see <http://learn.to/quote/> and <http://www.escape.de/users/tolot/mutt/> for more info. Have a nice day!\n";
55 $ftr_ad = undef; # too hard on performance to be default
56 $ftr_ml = undef; # too hard on performance to be default
57 $ofile = '-'; # use STDOUT if nothing is specified
58 $spass_prefix = 'SPAM: ';
59 $check_ratio = 0; # off by default
60 $msg_quote = "Blocked by $0: This message has been rejected because of a full quote. Please see http://learn.to/quote/ and http://www.escape.de/users/tolot/mutt/ for more info. Have a nice day!\n";
61 $msg_ratio = "Blocked by $0: This message has been rejected because of excessive quoting. Please see http://learn.to/quote/ and http://www.escape.de/users/tolot/mutt/ for more info. Have a nice day!\n";
62 # end of user adjusted vals
65 # set the defaults to the C locale
66 $mutt_attachment = '[-- Attachment #';
67 $mutt_contenttype = '[-- Type: ';
68 $mutt_pgpsigned = '[-- End of signed data --]';
69 $mutt_beginsigned = '[-- The following data is signed --]';
70 $mutt_pgpclearsigstart = '[-- BEGIN PGP SIGNED MESSAGE --]';
71 $mutt_pgpcryptend = '[-- END PGP MESSAGE --]';
72 $mutt_pgpcryptstart = '[-- BEGIN PGP MESSAGE --]';
73 $mutt_pgpclearsigned = '[-- END PGP SIGNED MESSAGE --]';
74 $mutt_pgpencrypted = '[-- End of PGP/MIME encrypted data --]';
75 $mutt_pgpoutstart = '[-- PGP output follows (current time:';
76 $mutt_pgpoutend = '[-- End of PGP output --]';
78 # set the defaults to the C locale
79 $gpg_WARNING = 'WARNING: ';
80 # (yes, the translations in gnupg's po files *are* braindamaged):
81 $gpg_Warning = 'WARNING: ';
82 $gpg_Cantcheck = 'Can\'t check signature: ';
84 $gpg_bad = 'BAD signature from ';
85 $gpg_expired = 'Note: This key has expired!';
86 $gpg_good = 'Good signature from';
87 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
88 $gpg_bug = '... this is a bug (';
91 # help(): print help text and exit with appropriate exit code
93 print "Usage: $0 [options]
94 -A=DIRECTORY ad footer directory, treat ad footers as signature
95 -a remove ad footers; requires -A
96 --bigq[=n[,x]] remove all but x lines of quotes with more than n
97 lines; default is n=30 and x=10
98 --body input has no headers; does not work with --pgp-short;
99 multipart messages will not be detected
100 -c[=n] merge multiple blank lines to n lines (default is 2)
101 --check[=FLAGS] check various criteria, print error message and quit;
102 see man page for details
103 -d, --debug print notice to syslog when bouncing; requires -p
104 --diff tolerate diffs
105 -e force ellipsis for excessive punctuation
106 --fixind fix quotes to adhere to RFC 3676
107 --ftr-ad enable aggressive ad footer matching; requires -A
108 --ftr-ml enable aggressive mailing list footer matching; req. -L
109 --groupwise delete Novell Groupwise style TOFU
110 -h, --help show this short help and exit
111 -i=INFILE file to be read; '-' for STDIN (default)
112 -k try to fix \"Kammquotes\"
113 --kdiff=n max. length difference between wrapped lines; req. -k
114 --kmaxl=n max. line length for wrapped line; requires -k
115 --kminl=n min. line length for wrapped line; requires -k
116 -L=DIRECTORY mailing list footer directory, treat mailing list
118 -l delete mailing list footer; requires -L
119 --lax-security use unsafe writing method; USE ON YOUR OWN RISK!
120 --locale=LOCALE internationalization; currently only used with -Mmutt
121 -M, --mua=MUA turn on special treatment for some mail user agents
122 -m delete MS style TOFU; careful: might be too aggressive
123 --max-lines=x maximum number of message lines
124 --ms-smart try to be smart with MS style TOFU; req. -Mmutt and -m
125 -o=OUTFILE file to be written to; '-' for STDOUT (default)
126 -P=MESSAGE user defined bounce message; requires -p
127 -p[=ADDRESS] redirect to ADDRESS if no TOFU was found
128 --pgp-move move pgp verification output to bottom; requires -Mmutt
129 --pgp-move-vrf move pgp output if verified and good; requires -Mmutt
130 --pgp-short hide non-relevant pgp key uids; requires -Mmutt
131 -r delete mail header lines
132 --reply squeeze multiple reply prefixes in subject line
133 -S[=n] suppress signatures with more than n lines (default $maxsig)
135 --sani sanitize some header fields
136 --sigsmax[=n] max number of sigs tolerated, no value for unlimited
137 --spass enable SpamAssassin workaround
138 -t delete traditional style TOFU
139 -v, --version show version string and exit
140 -w delete trailing whitespaces\n";
144 # version(): print version info and exit with appropriate exit code
146 print "$0 v".VER.REV." (Rev. ".REL."), Jochen Striepe <t-prot\@tolot.escape.de>
147 Get the latest version at <http://www.escape.de/users/tolot/mutt/>\n";
151 # sigint_handler(): what to do if we receive a single SIGINT
157 # Undoes MIME quoted-printable word encoding.
161 $word =~ s/=DF/ss/og;
162 $word =~ s/=C4/Ae/og;
163 $word =~ s/=D6/Oe/og;
164 $word =~ s/=DC/Ue/og;
165 $word =~ s/=E4/ae/og;
166 $word =~ s/=F6/oe/og;
167 $word =~ s/=FC/ue/og;
168 $word =~ s/=([0-9A-F]{2})/chr(hex $1)/ioge;
184 my $H = shift; # array of header lines
187 for ($i=0; $i<$#$H; $i++) {
188 if ($$H[$i] =~ /^subject: /io) {
190 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
191 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
194 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
198 while ($i<$#$H && $$H[$i] =~ /^\s/) {
200 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
201 $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
204 $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
209 elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
210 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
213 while ($i<$#$H && $$H[$i] =~ /^\s/) {
214 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
221 # remove_footers(): remove any trailing appearance of footers contained
222 # in the given directory.
224 my $L = shift; # array of message lines
225 my $S = shift; # array to store removed lines in
226 my $F = shift; # footers dir name
227 my $O = shift; # remove only one footer?
228 my $V = shift; # allow footers match before end of message
232 for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
235 if ($F && scalar(@$L)) {
236 if (!opendir(DIR, $F))
237 { print STDERR "Could not open $F: $!\n"; exit(EX_IOERR); }
238 my @feet = grep { /^[^.]/ && -f "$F/$_" } readdir DIR;
241 foreach my $f (@feet) {
242 if (!open(IN, "$F/$f"))
243 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
247 if (!scalar(@l)) { next; }
248 for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
251 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
252 if (scalar(@l)+$z<=scalar(@$L)) {
254 for(my $x=1; $x<=scalar(@l); $x++) {
255 if (index($$L[scalar(@$L)-$x-$z],
256 $l[scalar(@l)-$x])!=0) {
261 unshift(@$S, @$L[$#$L-$#l-$z..$#$L]);
262 splice(@$L, $#$L-$#l-$z);
263 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
264 unshift(@$S, pop(@$L));
266 if ($O) { last; } else { goto WIPE; }
272 while (scalar(@l)<=scalar(@$L)) {
273 for(my $x=1; $x<=scalar(@l); $x++) {
274 if (index($$L[scalar(@$L)-$x-$off], $l[scalar(@l)-$x])!=0) {
278 unshift(@$S, @$L[$#$L-$off-$#l..$#$L]);
279 splice(@$L, $#$L-$off-$#l);
280 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
281 unshift(@$S, pop(@$L));
291 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
294 my $L = shift; # array of message lines
295 my $V = shift; # array with verbatim list
296 my $max = 0; # plausible wraparound pos
298 # We scan the whole message first for a plausible common maximum line
299 # length where longer lines would be wrapped.
300 for (my $x=0; $x<$#$L; $x++) {
301 if ($$V[$x]!=1 && $max<length($$L[$x])) { $max = length($$L[$x]); }
304 # Next we see if there are plausible wraparounds.
305 for (my $x=0; $x+1<$#$L; $x++) {
308 # * it must not be verbatim,
309 # * the 2nd line must not be quoted nor empty nor just
310 # underlining some part of the line above (using '^')
311 # nor begin with a whitespace,
312 # * the 1st line must not end with a hyphen,
313 # * the 2nd line must not indicate content was deleted,
314 # * the 2nd line must not be some mutt(1) commentary,
315 # * there must not be a valid word wrap to produce a longer
316 # 1st line (if not quoted),
317 # * the 1st and 2nd line together must not be longer than some
318 # magical upper limit nor shorter than some magical lower
319 # bound nor nearly of the same length, and
320 # * the 3rd line must not be empty.
321 # With all that odds should be quite good that we have an
322 # automatedly wrapped line. Please send a note if you have
323 # additional good criteria. Thanks.
324 if (($$V[$x]!=1 && $$V[$x+1]!=1) &&
325 (index($$L[$x+1], $indent)!=0) &&
326 ($$L[$x+1] !~ /^$/) &&
327 ($$L[$x+1] !~ /^[\s^]/) &&
329 ($$L[$x+1] !~ /^\Q[...]\E\s*$/) &&
330 ($mua ne 'mutt' || $$L[$x+1] !~ /^(?:\e[^\a]+\a)?\[-- /) &&
331 (length($$L[$x])+index($$L[$x+1], ' ')>$max ||
332 (index($$L[$x+1], ' ')<0 &&
333 length($$L[$x])+length($$L[$x+1])>$max)) &&
334 (length($$L[$x])+length($$L[$x+1])<$kmaxl) &&
335 (length($$L[$x])+length($$L[$x+1])>$kminl) &&
336 (length($$L[$x])-length($$L[$x+1])>$kdiff) &&
337 ($$L[$x+2] !~ /^\s*$/))
339 $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
340 splice(@$L, $x+1, 1);
341 splice(@$V, $x+1, 1);
346 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
347 # but the last $x lines of them.
349 my $L = shift; # array of message lines
350 my $V = shift; # array with verbatim list
353 for (my $i=0; $i<=$#$L; $i++) {
360 if (index($$L[$i], $indent)==0) { $k++; } else {
365 $$L[$i] = "[---=| Quote block shrunk by $0: " .
366 "$x lines snipped |=---]\n";
368 splice(@$L, $i, $x-1);
369 splice(@$V, $i, $x-1);
378 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
383 my $L = shift; # message body
384 my $X = shift; # start line
385 my $Z = shift; # end line
390 if (index($$L[$X], "gpg: $gpg_WARNING")==0 ||
391 index($$L[$X], "gpg: $gpg_Warning")==0 ||
392 index($$L[$X], "gpg: $gpg_bad")==0 ||
393 index($$L[$X], "gpg: $gpg_Cantcheck")==0 ||
394 index($$L[$X], "gpg: $gpg_expired")==0 ||
395 index($$L[$X], "gpg: $gpg_bug")==0)
397 if (index($$L[$X], "gpg: $gpg_good")==0)
405 my $L = shift; # message body
406 my $V = shift; # verbatim list
407 my $H = shift; # headers
412 for (my $x=0; $x<scalar(@$L); $x++) {
413 if ($$V[$x]) { next; }
415 if ($$L[$x]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o)
418 for (my $m=0; $m<scalar(@$H); $m++) {
419 if (index($$H[$m], 'From:')==0) {
422 while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
423 { $from .= $$H[$m]; $m++; }
427 ($from) = $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
431 for (my $i=$x+1; $i<scalar(@$L); $i++) {
432 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
435 if ($uid>1 && index($$L[$i], $from)<0) {
441 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutend\E)/o) {
443 ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i)))
445 push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
446 splice(@$L, $x, $i-$x+2);
447 splice(@$V, $x, $i-$x+2);
448 $i -= $#{$tmp[$tmp]}-2;
458 (?:\Q$mutt_pgpencrypted\E |
459 \Q$mutt_pgpcryptend\E|
460 \Q$mutt_pgpclearsigned\E|
464 splice(@$L, $x+1, 0, @{$tmp[$tmp]});
465 for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
466 splice(@$V, $x+1, 0, (0));
468 $x += scalar(@{$tmp[$tmp--]});
474 push(@$L, @{$tmp[$tmp--]});
476 for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
480 # write_msg(): output
485 if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
486 if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) {
487 print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
490 elsif (!open(OUT, $O)) {
491 print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
504 # process_msg(): This one proc does *everything* what has to be done with
505 # the lines of the message
509 my ($j, $x, $verb) = (0, 0, 0);
510 my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) =
511 ((), (), (), (), (), (), (), (), ());
513 # Remove and store lines we might need later...
516 for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
517 @hdr = @$lines[0..$x];
518 splice(@$lines, 0, $x+1);
520 # remember the original body lines count
521 my $linecount = scalar(@$lines);
523 # Sanitize header fields:
524 if ($reply || $sani) { sanitize_hdr(\@hdr); }
527 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
530 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
532 # See if we have a multipart content type. If yes, see if it is already
533 # ripped (e.g. by you MUA, assuming it does not get special treatment with
534 # --mua=foo), otherwise only leave the first part if it is plain text (if
535 # not, we are done - non-text messages are not our business).
536 if ($mua ne 'mutt') {
537 for ($x=0; $x<scalar(@hdr); $x++) {
538 if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
541 if ($foo =~ m!^multipart/!) {
544 if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
546 for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
547 if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) {
555 for (my $x=0; $x<scalar(@$lines); $x++) {
556 if (index($$lines[$x], '--'.$foo)!=0) { next; }
558 my $bar = 'text/plain';
559 for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
561 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) {
565 if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
567 if ($bar =~ m!^text/plain!) {
569 for ($z=1; $x+$z<@$lines; $z++) {
570 if (index($$lines[$x+$z], '--'.$foo)==0) {
574 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
576 @bo2 = @$lines[$x+$z..$#$lines];
577 splice(@$lines, $x+$z);
578 if ($$lines[$#$lines] =~ /^\s*$/) {
579 unshift(@bo2, pop @$lines);
581 @bo1 = @$lines[0..$x];
582 splice(@$lines, 0, $x+1);
584 # remove mailing list and ad footers within this
586 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
587 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
592 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
593 ($hdrs?undef:\@hdr), $lines);
607 for ($x=0; $x<scalar(@$lines); $x++) {
608 if ($$lines[$x] =~ /^\s*#v([+-])$/) {
609 $verb = $1 eq '+' ? 1 : 0;
611 } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/) {
612 # Detect and protect standard diffs.
613 # Skip and proceed to next line if premature file end or diff line
614 # numbers implausible (so it might be not a diff at all).
615 if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
617 for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
618 } elsif ($diff && $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/) {
619 if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
621 for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
622 } else { $vrb[$x] = $verb; }
625 # Calculate quoting ratio (with respect to verbatims):
626 if ($check && scalar(@$lines)) {
627 my ($y, $z) = (0, 0);
628 for ($x=0; $x<scalar(@$lines); $x++) {
631 if (index($$lines[$x], $indent)==0) { $y++; }
635 if ($y/$z>=$check_ratio) {
641 if ($mua eq 'mutt') {
642 # See if we find pgp output generated by mutt before we scramble
643 # the thing. If yes, see if we can beautify it.
644 if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
646 # Remove all but the first attachment (if this is text/plain)
647 # mutt did introduce (bah!). Remember, all this ugliness could
648 # be replaced with a proper and clean edit_filter patch in
650 for ($x=$#$lines; $x>=0; $x--) {
651 if ($vrb[$x]) { next; }
652 # The following regexp's are quite ugly because for most users
653 # these lines are coloured using termcap... (bah!)
655 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
660 (?:\Q$mutt_contenttype\E)
661 (?:text/plain|application/pgp)
665 (?:\Q$mutt_pgpsigned\E |
666 \Q$mutt_pgpclearsigned\E|
667 \Q$mutt_pgpcryptend\E|
668 \Q$mutt_pgpencrypted\E)
671 # Strip attachments to prepare further processing
672 unshift(@att, @$lines[$x..$#$lines]);
674 # Try to fix trailing empty lines
675 while (scalar(@$lines) && $$lines[$#$lines] =~
676 /^(?:\e[^\a]+\a)?\s*$/) {
677 unshift(@att, pop(@$lines));
680 # Remove ML and ad footers within attachments:
682 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
683 if ($ad) { remove_footers($lines, \@tmp, $ads, undef); }
684 $x = scalar(@$lines);
688 # care about the rest
690 for ($x=0; $x<$#att; $x++) {
691 if ($vrb[scalar(@$lines)+$x]) { next; }
693 # Pipe message/rfc822 parts to another instance of
694 # process_msg() for further processing.
695 # Please note that we cannot see what a hierarchy the
696 # original message had -- if there were message/rfc822
697 # parts within other message/rfc822 parts constellations
698 # can occur which we cannot resolve. Therefore we simply
699 # do not even try to be smart. This should work for most
701 # The following regexp is quite ugly because for most
702 # users the line is coloured using termcap... (bah!)
704 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
707 (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
711 while ($att[$x] !~ /^\s*$/) { $x++; }
714 my @tmp = @att[$x..$#att];
716 splice(@att, $x, scalar(@att)-$x, @tmp);
724 if (scalar(@$lines)) {
729 for ($x = $#$lines; $x>=0; $x--) {
731 if ($$lines[$x] =~ /^-- $/) {
733 for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
734 if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
735 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
738 unshift(@sig, @$lines[$x..$#$lines]);
744 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
748 if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
749 if ($lsig && !$sig) {
750 unshift(@sig, "[---=| Overlong signature removed by $0: " .
751 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
756 unshift(@sig, @$lines[$x..$#$lines]);
759 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
761 # any trailing newlines?
762 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
763 elsif ($chk_empty) { $chk_empty = 0; }
768 # Fix quote markers to adhere to RFC 3676, this changes "> >" to ">> " (if
769 # the default $indent is used). The space after ">" is not mandatory by RFC
770 # but makes the result more readable.
772 for ($x=0; $x<scalar(@$lines); $x++) {
773 if (!$vrb[$x] && index($$lines[$x], $indent)==0) {
774 # We match space at the beginning to prevent removal of spaces
775 # directly after the last quote mark.
776 $$lines[$x] =~ /^((\Q$indent\E| )*\Q$indent\E ?)(.*)$/;
779 my $rest = $3; # if anything follows after the quote
781 $tmp .= ' ' if $rest; # don't create trailing whitespace
782 substr($$lines[$x], 0, $len, $tmp);
787 # See if there is some Kammquoting to fix:
788 if ($kamm) { decomb($lines, \@vrb); }
790 # Now care about TOFU.
791 # One common mispractice is M$ and Groupwise style TOFU:
793 # bloat this array if you want more internationalization:
794 my @tofu = ('Original Message',
795 'Original-Nachricht',
796 'Ursprüngliche Nachricht',
797 'Ursprüngliche Nachricht',
798 'Ursprungliche Nachricht',
800 'Ursprungligt meddelande',
801 'Oorspronkelijk bericht',
802 'Message d\'origine',
804 'Weitergeleitete Nachricht / Forwarded Message',
805 '[A-Za-z ]+ <(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})> schrieb:');
806 my $k = 0; # any text above?
807 my $tmp = 0; # flagged if inside PGP output
809 DONE: for ($x=0; $x<scalar(@$lines); $x++) {
811 foreach my $tmp (@tofu) {
812 if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
814 ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
815 $$lines[$x] =~ /^>>> On [A-Z][a-z][a-z]?, [A-Z][a-z][a-z]? \d\d?, \d\d\d\d at [ \d]\d:\d\d [AP]M, in message/))) {
823 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
826 # this line seems not necessary since mutt-1.5.18:
827 $$lines[$x] !~ /^\e[^\a]+\a$/o &&
829 /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
831 /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
832 ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
834 if ($mua eq 'mutt' && (!$tmp) &&
836 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
838 } elsif ($mua eq 'mutt' && $tmp &&
840 /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
842 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpcryptstart\E)/o ||
844 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
853 # try to avoid false positives and only delete m$ style tofu if
854 # there is text above
856 if (!$ms_smart) { goto CLEAN; }
858 # first, see if there is pgp stuff inside the tofu:
859 my $p = 0; # levels of pgp signed parts
861 for (my $i=$x+1; $i<scalar(@$lines); $i++) {
863 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
868 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
869 if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
874 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
875 splice(@att, 0, $i+1);
883 # now removing is safe:
884 CLEAN: $j = scalar(@$lines)-$x;
889 # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
890 # much more aggressively, so we won't need to search any more if we
892 if ($trad && (!$j) && !$vrb[$#$lines]) {
893 if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) {
894 unshift(@sig, pop(@$lines));
900 for (my $i=$#$lines; $i>=0; $i--) {
901 if ($vrb[$i]) { last; }
902 if (index($$lines[$i], $indent)==0) {
906 elsif ($$lines[$i] !~ /^\s*$/) { last; }
910 # if there is no text above, we will assume the message is meant
911 # as forwarding and therefore OK
912 for (my $i=$k-1; $i>=0; $i--) {
913 if ($$lines[$i] !~ /^\s*$/o) {
926 # OK, if we found TOFU, we will leave a message that we were here...
928 # make sendmail bounce if we shall be picky
929 # and indeed found something:
935 eval { require Sys::Syslog; };
936 if ($@) { warn $@; } else {
937 Sys::Syslog::setlogsock('unix');
938 Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
939 Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
940 Sys::Syslog::closelog();
948 # if we were invoked just for checking and indeed found something,
949 # print out the error message and quit:
955 push(@$lines, "[---=| TOFU protection by $0: " .
956 "$j lines snipped |=---]\n");
958 elsif ($mda eq '1') { exit EX_OK; }
960 # Care for huge blocks of quoted original message:
961 if ($bigqn) { debigq($lines, \@vrb); }
963 # Care for trailing whitespaces:
965 for ($x=0; $x<scalar(@$lines); $x++) {
966 if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
970 # Care for punctuation abuse:
972 for ($x=0; $x<scalar(@$lines); $x++) {
973 if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
977 # (Nearly) at last care for multiple blank lines. (Do not do this
978 # earlier -- the way it is done right now would screw up the verbatim
981 # When handling regular TOFU above we move the last empty line from
982 # the body to the signature. This prevents the correct removal of
983 # empty lines before a signature (one line less is removed than it
984 # should) when -c and -t are used. This fixes it.
985 if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
986 push(@$lines, shift(@sig));
990 for ($x=scalar(@$lines)-1; $x>=0; $x--) {
992 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
993 $$lines[$x] =~ /^\s*$/o))
995 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
1001 # Everything changing the body is done now. Time to fix the line count
1002 # header so naive clients do not get confused. Just to be sure, append
1003 # the old line count to X-headers.
1004 my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
1005 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) +
1006 (!$ad?scalar(@ads):0);
1007 if ($linecount-$l!=0) {
1008 for ($x=0; $x<scalar(@hdr); $x++) {
1010 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1012 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1018 # Finally, before leaving we put everything back in right order.
1019 unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1020 push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1026 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1027 ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1029 # command line switches
1030 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1031 $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1032 $sani, $sig, $sigint, $spass, $trad, $trsp) =
1033 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1034 my $ifile = '-'; # use STDIN if nothing specified
1037 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1038 $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1040 # get command line params:
1041 if (!Getopt::Long::GetOptions(
1044 'bigq:s' => \$_t_bigq,
1045 'body' => \$_t_nohdr,
1047 'check:s' => \$_t_check,
1048 'debug|d' => \$sysl,
1051 'fixind' => \$fixind,
1052 'ftr-ad' => \$ftr_ad,
1053 'ftr-ml' => \$ftr_ml,
1054 'groupwise' => \$gw,
1055 'help|h' => \$_t_help,
1058 'kminl=i' => \$_t_kminl,
1059 'kmaxl=i' => \$_t_kmaxl,
1060 'kdiff=i' => \$_t_kdiff,
1063 'lax-security' => \$lax,
1064 'locale=s' => \$locale,
1065 'max-lines:i' => \$maxlines,
1066 'ms-smart' => \$ms_smart,
1067 'mua|M=s' => \$_t_mua,
1071 'p:s' => \$_t_redir,
1072 'pgp-short' => \$pgpshort,
1073 'pgp-move' => \$pgpmove,
1074 'pgp-move-vrf' => \$pgpmovevrf,
1077 'S:i' => \$_t_maxsig,
1080 'sigsmax:i' => \$_t_sigsmax,
1083 'version|v' => \$_t_ver,
1089 # clean up temp vals:
1090 if (defined $_t_bigq) {
1091 if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1094 if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1096 if (defined $_t_help) { help(); }
1097 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1098 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1099 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1100 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1101 if (defined $_t_mua) {
1103 # mutt still displays the message when ^C'ing pgp verification:
1104 if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1106 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1107 if (defined $_t_check) {
1109 while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1112 if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1115 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1116 if (defined $_t_ver) { version(); }
1117 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1118 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1119 if ($ms_smart) { $ms = 1; }
1121 if ($mua eq 'mutt') {
1122 if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1123 eval { require Locale::gettext; };
1124 if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1125 Locale::gettext::textdomain('mutt');
1126 ($mutt_attachment) =
1127 Locale::gettext::gettext("[-- Attachment #%d") =~
1129 ($mutt_contenttype) =
1130 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1133 Locale::gettext::gettext("[-- End of signed data --]\n") =~
1135 ($mutt_beginsigned) =
1136 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1138 ($mutt_pgpclearsigned) =
1139 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1141 ($mutt_pgpclearsigstart) =
1142 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1144 ($mutt_pgpencrypted) =
1145 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1147 ($mutt_pgpoutstart) =
1148 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1151 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1154 Locale::gettext::textdomain('gnupg');
1156 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1159 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1162 Locale::gettext::gettext("Can't check signature: %s\n") =~
1165 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1168 Locale::gettext::gettext('BAD signature from "%s"') =~
1171 Locale::gettext::gettext("Note: This key has expired!\n") =~
1174 Locale::gettext::gettext('Good signature from "%s"') =~
1175 m/^(([^"«%]*))["«%]/;
1177 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1183 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1185 if (($ml && $footers eq '')|| # no -l without -L
1186 ($ad && $ads eq '')|| # no -a without -A
1187 ($nohdr && $pgpshort)|| # --body and --pgp-short are like oil and water
1188 (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1189 ($ifile eq '')|| # no empty -i
1190 ($ofile eq '')) # no empty -o
1195 if (!open(IN, $ifile))
1196 { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1200 # First, check msg length and stop processing if msg is too long:
1201 if ((defined $maxlines) && (@message > $maxlines)) {
1202 if ($mua eq 'mutt') {
1205 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1208 splice(@message, $x, 0,
1209 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1211 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1213 if ($mua eq 'mutt') { exit(EX_OK); }
1217 # this should be self-explanatory:
1218 process_msg(\@message);
1220 # Finally, print clean lines:
1221 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1223 # vim600:set foldmethod=marker: