2 # $Id: t-prot,v 1.298 2010/03/06 01:32:50 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.97';
9 use constant REV => '';
10 use constant REL => q$Revision: 1.298 $=~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 $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
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, regexp 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_pgpclearsigned = '[-- END PGP SIGNED MESSAGE --]';
72 $mutt_pgpencrypted = '[-- End of PGP/MIME encrypted data --]';
73 $mutt_pgpoutstart = '[-- PGP output follows (current time:';
74 $mutt_pgpoutend = '[-- End of PGP output --]';
76 # set the defaults to the C locale
77 $gpg_WARNING = 'WARNING: ';
78 # (yes, the translations in gnupg's po files *are* braindamaged):
79 $gpg_Warning = 'WARNING: ';
80 $gpg_Cantcheck = 'Can\'t check signature: ';
82 $gpg_bad = 'BAD signature from ';
83 $gpg_expired = 'Note: This key has expired!';
84 $gpg_good = 'Good signature from';
85 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
86 $gpg_bug = '... this is a bug (';
89 # help(): print help text and exit with appropriate exit code
91 print "Usage: $0 [options]
92 -A DIRECTORY ad footer directory, treat ad footers as signature
93 -a remove ad footers; requires -A
94 --bigq[=n[,x]] remove all but x lines of quotes with more than n
95 lines; default is n=30 and x=10
96 --body input has no headers; does not work with --pgp-short;
97 multipart messages will not be detected
98 -c[n] merge multiple blank lines to n lines (default is 2)
99 --check[=FLAGS] check various criteria, print error message and quit;
100 see man page for details
101 -d, --debug print notice to syslog when bouncing; requires -p
102 --diff tolerate diffs
103 -e force ellipsis for excessive punctuation
104 --ftr-ad enable aggressive ad footer matching; requires -A
105 --ftr-ml enable aggressive mailing list footer matching; req. -L
106 --groupwise delete Novell Groupwise style TOFU
107 -h, --help show this short help and exit
108 -i INFILE file to be read; '-' for STDIN (default)
109 -k try to fix \"Kammquotes\"
110 --kdiff=n max. length difference between wrapped lines; req. -k
111 --kmaxl=n max. line length for wrapped line; requires -k
112 --kminl=n min. line length for wrapped line; requires -k
113 -L DIRECTORY mailing list footer directory, treat mailing list
115 -l delete mailing list footer; requires -L
116 --lax-security use unsafe writing method; USE ON YOUR OWN RISK!
117 --locale=LOCALE internationalization; currently only used with -Mmutt
118 -M, --mua=MUA turn on special treatment for some mail user agents
119 -m delete MS style TOFU; careful: might be too agressive
120 --max-lines=x maximum number of message lines
121 --ms-smart try to be smart with MS style TOFU; req. -Mmutt and -m
122 -o OUTFILE file to be written to; '-' for STDOUT (default)
123 -P MESSAGE user defined bounce message; requires -p
124 -p[ADDRESS] redirect to ADDRESS if no TOFU was found
125 --pgp-move move pgp verification output to bottom; requires -Mmutt
126 --pgp-move-vrf move pgp output if verified and good; requires -Mmutt
127 --pgp-short hide non-relevant pgp key uids; requires -Mmutt
128 -r delete mail header lines
129 --reply squeeze multiple reply prefixes in subject line
130 -S[n] supress signatures with more than n lines (default $maxsig)
132 --sani sanitize some header fields
133 --sigsmax[=n] max number of sigs tolerated, no value for unlimited
134 --spass enable SpamAssassin workaround
135 -t delete traditional style TOFU
136 -v, --version show version string and exit
137 -w delete trailing whitespaces\n";
141 # version(): print version info and exit with appropriate exit code
143 print "$0 v".VER.REV." (Rev. ".REL."), Jochen Striepe <t-prot\@tolot.escape.de>
144 Get the latest version at <http://www.escape.de/users/tolot/mutt/>\n";
148 # sigint_handler(): what to do if we receive a single SIGINT
154 # Undoes MIME quoted-printable word encoding.
158 $word =~ s/=DF/ss/og;
159 $word =~ s/=C4/Ae/og;
160 $word =~ s/=D6/Oe/og;
161 $word =~ s/=DC/Ue/og;
162 $word =~ s/=E4/ae/og;
163 $word =~ s/=F6/oe/og;
164 $word =~ s/=FC/ue/og;
165 $word =~ s/=([0-9A-F]{2})/chr(hex $1)/ioge;
181 my $H = shift; # array of header lines
184 for ($i=0; $i<$#$H; $i++) {
185 if ($$H[$i] =~ /^subject: /io) {
187 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
188 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
191 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
195 while ($i<$#$H && $$H[$i] =~ /^\s/) {
197 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
198 $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
201 $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
206 elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
207 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
210 while ($i<$#$H && $$H[$i] =~ /^\s/) {
211 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
218 # remove_footers(): remove any trailing appearance of footers contained
219 # in the given directory.
221 my $L = shift; # array of message lines
222 my $S = shift; # array to store removed lines in
223 my $F = shift; # footers dir name
224 my $O = shift; # remove only one footer?
225 my $V = shift; # allow footers match before end of message
229 for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
232 if ($F && scalar(@$L)) {
233 if (!opendir(DIR, $F))
234 { print STDERR "Could not open $F: $!\n"; exit(EX_IOERR); }
235 my @feet = grep { /^[^.]/ && -f "$F/$_" } readdir DIR;
238 foreach my $f (@feet) {
239 if (!open(IN, "$F/$f"))
240 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
244 if (!scalar(@l)) { next; }
245 for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
248 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
249 if (scalar(@l)+$z<=scalar(@$L)) {
251 for(my $x=1; $x<=scalar(@l); $x++) {
252 if (index($$L[scalar(@$L)-$x-$z],
253 $l[scalar(@l)-$x])!=0) {
258 unshift(@$S, @$L[$#$L-$#l-$z..$#$L]);
259 splice(@$L, $#$L-$#l-$z);
260 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
261 unshift(@$S, pop(@$L));
263 if ($O) { last; } else { goto WIPE; }
269 while (scalar(@l)<=scalar(@$L)) {
270 for(my $x=1; $x<=scalar(@l); $x++) {
271 if (index($$L[scalar(@$L)-$x-$off], $l[scalar(@l)-$x])!=0) {
275 unshift(@$S, @$L[$#$L-$off-$#l..$#$L]);
276 splice(@$L, $#$L-$off-$#l);
277 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
278 unshift(@$S, pop(@$L));
288 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
291 my $L = shift; # array of message lines
292 my $V = shift; # array with verbatim list
293 my $max = 0; # plausible wraparound pos
295 # We scan the whole message first for a plausible common maximum line
296 # length where longer lines would be wrapped.
297 for (my $x=0; $x<$#$L; $x++) {
298 if ($$V[$x]!=1 && $max<length($$L[$x])) { $max = length($$L[$x]); }
301 # Next we see if there are plausible wraparounds.
302 for (my $x=0; $x+1<$#$L; $x++) {
305 # * it must not be verbatim,
306 # * the 2nd line must not be quoted nor empty nor just
307 # underlining some part of the line above (using '^')
308 # nor begin with a whitespace,
309 # * the 1st line must not end with a hyphen,
310 # * the 2nd line must not indicate content was deleted,
311 # * the 2nd line must not be some mutt(1) commentary,
312 # * there must not be a valid word wrap to produce a longer
313 # 1st line (if not quoted),
314 # * the 1st and 2nd line together must not be longer than some
315 # magical upper limit nor shorter than some magical lower
316 # bound nor nearly of the same length, and
317 # * the 3rd line must not be empty.
318 # With all that odds should be quite good that we have an
319 # automatedly wrapped line. Please send a note if you have
320 # additional good criteria. Thanks.
321 if (($$V[$x]!=1 && $$V[$x+1]!=1) &&
322 (index($$L[$x+1], $indent)!=0) &&
323 ($$L[$x+1] !~ /^$/) &&
324 ($$L[$x+1] !~ /^[\s^]/) &&
326 ($$L[$x+1] !~ /^\Q[...]\E\s*$/) &&
327 ($mua ne 'mutt' || $$L[$x+1] !~ /^(?:\e[^\a]+\a)?\[-- /) &&
328 (length($$L[$x])+index($$L[$x+1], ' ')>$max ||
329 (index($$L[$x+1], ' ')<0 &&
330 length($$L[$x])+length($$L[$x+1])>$max)) &&
331 (length($$L[$x])+length($$L[$x+1])<$kmaxl) &&
332 (length($$L[$x])+length($$L[$x+1])>$kminl) &&
333 (length($$L[$x])-length($$L[$x+1])>$kdiff) &&
334 ($$L[$x+2] !~ /^\s*$/))
336 $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
337 splice(@$L, $x+1, 1);
338 splice(@$V, $x+1, 1);
343 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
344 # but $x lines of them.
346 my $L = shift; # array of message lines
347 my $V = shift; # array with verbatim list
350 for (my $i=0; $i<=$#$L; $i++) {
357 if (index($$L[$i], $indent)==0) { $k++; } else {
362 $$L[$i] = "[---=| Quote block shrunk by $0: " .
363 "$x lines snipped |=---]\n";
365 splice(@$L, $i, $x-1);
366 splice(@$V, $i, $x-1);
375 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
380 my $L = shift; # message body
381 my $X = shift; # start line
382 my $Z = shift; # end line
387 if (index($$L[$X], "gpg: $gpg_WARNING")==0 ||
388 index($$L[$X], "gpg: $gpg_Warning")==0 ||
389 index($$L[$X], "gpg: $gpg_bad")==0 ||
390 index($$L[$X], "gpg: $gpg_Cantcheck")==0 ||
391 index($$L[$X], "gpg: $gpg_expired")==0 ||
392 index($$L[$X], "gpg: $gpg_bug")==0)
394 if (index($$L[$X], "gpg: $gpg_good")==0)
402 my $L = shift; # message body
403 my $V = shift; # verbatim list
404 my $H = shift; # headers
409 for (my $x=0; $x<scalar(@$L); $x++) {
410 if ($$V[$x]) { next; }
412 if ($$L[$x]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o)
415 for (my $m=0; $m<scalar(@$H); $m++) {
416 if (index($$H[$m], 'From:')==0) {
419 while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
420 { $from .= $$H[$m]; $m++; }
424 ($from) = $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
428 for (my $i=$x+1; $i<scalar(@$L); $i++) {
429 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
432 if ($uid>1 && index($$L[$i], $from)<0) {
438 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutend\E)/o) {
440 ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i)))
442 push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
443 splice(@$L, $x, $i-$x+2);
444 splice(@$V, $x, $i-$x+2);
445 $i -= $#{$tmp[$tmp]}-2;
455 (?:\Q$mutt_pgpencrypted\E |
456 \Q$mutt_pgpclearsigned\E|
460 splice(@$L, $x+1, 0, @{$tmp[$tmp]});
461 for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
462 splice(@$V, $x+1, 0, (0));
464 $x += scalar(@{$tmp[$tmp--]});
470 push(@$L, @{$tmp[$tmp--]});
472 for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
476 # write_msg(): output
481 if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
482 if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) {
483 print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
486 elsif (!open(OUT, $O)) {
487 print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
500 # process_msg(): This one proc does *everything* what has to be done with
501 # the lines of the message
505 my ($j, $x, $verb) = (0, 0, 0);
506 my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) =
507 ((), (), (), (), (), (), (), (), ());
509 # Remove and store lines we might need later...
512 for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
513 @hdr = @$lines[0..$x];
514 splice(@$lines, 0, $x+1);
516 # remember the original body lines count
517 my $linecount = scalar(@$lines);
519 # Sanitize header fields:
520 if ($reply || $sani) { sanitize_hdr(\@hdr); }
523 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
526 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
528 # See if we have a multipart content type. If yes, see if it is already
529 # ripped (e.g. by you MUA, assuming it does not get special treatment with
530 # --mua=foo), otherwise only leave the first part if it is plain text (if
531 # not, we are done - non-text messages are not our business).
532 if ($mua ne 'mutt') {
533 for ($x=0; $x<scalar(@hdr); $x++) {
534 if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
537 if ($foo =~ m!^multipart/!) {
540 if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
542 for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
543 if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) {
551 for (my $x=0; $x<scalar(@$lines); $x++) {
552 if (index($$lines[$x], '--'.$foo)!=0) { next; }
554 my $bar = 'text/plain';
555 for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
557 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) {
561 if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
563 if ($bar =~ m!^text/plain!) {
565 for ($z=1; $x+$z<@$lines; $z++) {
566 if (index($$lines[$x+$z], '--'.$foo)==0) {
570 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
572 @bo2 = @$lines[$x+$z..$#$lines];
573 splice(@$lines, $x+$z);
574 if ($$lines[$#$lines] =~ /^\s*$/) {
575 unshift(@bo2, pop @$lines);
577 @bo1 = @$lines[0..$x];
578 splice(@$lines, 0, $x+1);
580 # remove mailing list and ad footers within this
582 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
583 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
588 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
589 ($hdrs?undef:\@hdr), $lines);
603 for ($x=0; $x<scalar(@$lines); $x++) {
604 if ($$lines[$x] =~ /^\s*#v([+-])$/) {
605 $verb = $1 eq '+' ? 1 : 0;
607 } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/) {
608 # Detect and protect standard diffs.
609 # Skip and proceed to next line if premature file end or diff line
610 # numbers implausible (so it might be not a diff at all).
611 if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
613 for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
614 } elsif ($diff && $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/) {
615 if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
617 for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
618 } else { $vrb[$x] = $verb; }
621 # Calculate quoting ratio (with respect to verbatims):
622 if ($check && scalar(@$lines)) {
623 my ($y, $z) = (0, 0);
624 for ($x=0; $x<scalar(@$lines); $x++) {
627 if (index($$lines[$x], $indent)==0) { $y++; }
631 if ($y/$z>=$check_ratio) {
637 if ($mua eq 'mutt') {
638 # See if we find pgp output generated by mutt before we scramble
639 # the thing. If yes, see if we can beautify it.
640 if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
642 # Remove all but the first attachment (if this is text/plain)
643 # mutt did introduce (bah!). Remember, all this ugliness could
644 # be replaced with a proper and clean edit_filter patch in
646 for ($x=$#$lines; $x>=0; $x--) {
647 if ($vrb[$x]) { next; }
648 # The following regexp's are quite ugly because for most users
649 # these lines are coloured using termcap... (bah!)
651 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
656 (?:\Q$mutt_contenttype\E)
657 (?:text/plain|application/pgp)
661 (?:\Q$mutt_pgpsigned\E |
662 \Q$mutt_pgpclearsigned\E|
663 \Q$mutt_pgpencrypted\E)
666 # Strip attachments to prepare further processing
667 unshift(@att, @$lines[$x..$#$lines]);
669 # Try to fix trailing empty lines
670 while (scalar(@$lines) && $$lines[$#$lines] =~
671 /^(?:\e[^\a]+\a)?\s*$/) {
672 unshift(@att, pop(@$lines));
675 # Remove ML and ad footers within attachments:
677 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
678 if ($ad) { remove_footers($lines, \@tmp, $ads, undef); }
679 $x = scalar(@$lines);
683 # care about the rest
685 for ($x=0; $x<$#att; $x++) {
686 if ($vrb[scalar(@$lines)+$x]) { next; }
688 # Pipe message/rfc822 parts to another instance of
689 # process_msg() for further processing.
690 # Please note that we cannot see what a hierarchy the
691 # original message had -- if there were message/rfc822
692 # parts within other message/rfc822 parts constellations
693 # can occur which we cannot resolve. Therefore we simply
694 # do not even try to be smart. This should work for most
696 # The following regexp is quite ugly because for most
697 # users the line is coloured using termcap... (bah!)
699 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
702 (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
706 while ($att[$x] !~ /^\s*$/) { $x++; }
709 my @tmp = @att[$x..$#att];
711 splice(@att, $x, scalar(@att)-$x, @tmp);
719 if (scalar(@$lines)) {
724 for ($x = $#$lines; $x>=0; $x--) {
726 if ($$lines[$x] =~ /^-- $/) {
728 for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
729 if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
730 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
733 unshift(@sig, @$lines[$x..$#$lines]);
739 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
743 if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
744 if ($lsig && !$sig) {
745 unshift(@sig, "[---=| Overlong signature removed by $0: " .
746 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
751 unshift(@sig, @$lines[$x..$#$lines]);
754 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
756 # any trailing newlines?
757 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
758 elsif ($chk_empty) { $chk_empty = 0; }
763 # See if there is some Kammquoting to fix:
764 if ($kamm) { decomb($lines, \@vrb); }
766 # Now care about TOFU.
767 # One common mispractice is M$ and Groupwise style TOFU:
769 # bloat this array if you want more internationalization:
770 my @tofu = ('Original Message',
771 'Original-Nachricht',
772 'Ursprüngliche Nachricht',
773 'Ursprüngliche Nachricht',
774 'Ursprungliche Nachricht',
776 'Ursprungligt meddelande',
777 'Oorspronkelijk bericht',
778 'Message d\'origine',
780 'Weitergeleitete Nachricht / Forwarded Message');
781 my $k = 0; # any text above?
782 my $tmp = 0; # flagged if inside PGP output
784 DONE: for ($x=0; $x<scalar(@$lines); $x++) {
786 foreach my $tmp (@tofu) {
787 if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
789 ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
790 $$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/))) {
798 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
801 # this line seems not necessary since mutt-1.5.18:
802 $$lines[$x] !~ /^\e[^\a]+\a$/o &&
804 /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
806 /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
807 ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
809 if ($mua eq 'mutt' && (!$tmp) &&
811 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
813 } elsif ($mua eq 'mutt' && $tmp &&
815 /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
817 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
826 # try to avoid false positives and only delete m$ style tofu if
827 # there is text above
829 if (!$ms_smart) { goto CLEAN; }
831 # first, see if there is pgp stuff inside the tofu:
832 my $p = 0; # levels of pgp signed parts
834 for (my $i=$x+1; $i<scalar(@$lines); $i++) {
836 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
841 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
842 if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
847 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
848 splice(@att, 0, $i+1);
856 # now removing is safe:
857 CLEAN: $j = scalar(@$lines)-$x;
862 # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
863 # much more aggressively, so we won't need to search any more if we
865 if ($trad && (!$j) && !$vrb[$#$lines]) {
866 if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) {
867 unshift(@sig, pop(@$lines));
873 for (my $i=$#$lines; $i>=0; $i--) {
874 if ($vrb[$i]) { last; }
875 if ($$lines[$i] =~ /^$indent/o) {
879 elsif ($$lines[$i] !~ /^\s*$/) { last; }
883 # if there is no text above, we will assume the message is meant
884 # as forwarding and therefore OK
885 for (my $i=$k-1; $i>=0; $i--) {
886 if ($$lines[$i] !~ /^\s*$/o) {
899 # OK, if we found TOFU, we will leave a message that we were here...
901 # make sendmail bounce if we shall be picky
902 # and indeed found something:
908 eval { require Sys::Syslog; };
909 if ($@) { warn $@; } else {
910 Sys::Syslog::setlogsock('unix');
911 Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
912 Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
913 Sys::Syslog::closelog();
921 # if we were invoked just for checking and indeed found something,
922 # print out the error message and quit:
928 push(@$lines, "[---=| TOFU protection by $0: " .
929 "$j lines snipped |=---]\n");
931 elsif ($mda eq '1') { exit EX_OK; }
933 # Care for huge blocks of quoted original message:
934 if ($bigqn) { debigq($lines, \@vrb); }
936 # Care for trailing whitespaces:
938 for ($x=0; $x<scalar(@$lines); $x++) {
939 if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
943 # Care for punctuation abuse:
945 for ($x=0; $x<scalar(@$lines); $x++) {
946 if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
950 # (Nearly) at last care for multiple blank lines. (Do not do this
951 # earlier -- the way it is done right now would screw up the verbatim
955 for ($x=scalar(@$lines)-1; $x>=0; $x--) {
957 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
958 $$lines[$x] =~ /^\s*$/o))
960 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
966 # Everything changing the body is done now. Time to fix the line count
967 # header so naive clients do not get confused. Just to be sure, append
968 # the old line count to X-headers.
969 my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
970 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) +
971 (!$ad?scalar(@ads):0);
972 if ($linecount-$l!=0) {
973 for ($x=0; $x<scalar(@hdr); $x++) {
975 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
977 $hdr[$#hdr] = "X-Old-Lines: $2\n";
983 # Finally, before leaving we put everything back in right order.
984 unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
985 push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
991 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
992 ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
994 # command line switches
995 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $footers, $lax,
996 $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
997 $sani, $sig, $sigint, $spass, $trad, $trsp) =
998 (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);
999 my $ifile = '-'; # use STDIN if nothing specified
1002 my ($_t1, $_t2, $_t3, $_t4, $_t5, $_t6, $_t7, $_t8, $_t9, $_ta, $_tb, $_tc,
1005 # get command line params:
1006 if (!Getopt::Long::GetOptions(
1013 'debug|d' => \$sysl,
1016 'ftr-ad' => \$ftr_ad,
1017 'ftr-ml' => \$ftr_ml,
1018 'groupwise' => \$gw,
1027 'lax-security' => \$lax,
1028 'locale=s' => \$locale,
1029 'max-lines:i' => \$maxlines,
1030 'ms-smart' => \$ms_smart,
1036 'pgp-short' => \$pgpshort,
1037 'pgp-move' => \$pgpmove,
1038 'pgp-move-vrf' => \$pgpmovevrf,
1044 'sigsmax:i' => \$_ta,
1047 'version|v' => \$_tb,
1053 # clean up temp vals:
1055 if ($_t1 !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1058 if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1060 if (defined $_t2) { help(); }
1061 if (defined $_t3) { $kminl = $_t3; $kamm = 1; }
1062 if (defined $_t4) { $kmaxl = $_t4; $kamm = 1; }
1063 if (defined $_t5) { $kdiff = $_t5; $kamm = 1; }
1064 if (defined $_t6) { $lsig = $_t6 ? $_t6 : $maxsig; }
1067 # mutt still displays the message when ^C'ing pgp verification:
1068 if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1070 if (defined $_t8) { $mda = $_t8 ? $_t8 : '1'; }
1073 while ($_t9 && $_t9 =~ /^([^,\s]+)(?:,(\S+))?$/) {
1076 if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1079 if (defined $_ta) { $sign = $_ta ? $_ta : undef; }
1080 if (defined $_tb) { version(); }
1081 if (defined $_tc) { $nohdr=1; $hdrs=1; }
1082 if (defined $_td) { $cr = $_td ? $_td : $crshrink; }
1083 if ($ms_smart) { $ms = 1; }
1085 if ($mua eq 'mutt') {
1086 if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1087 eval { require Locale::gettext; };
1088 if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1089 Locale::gettext::textdomain('mutt');
1090 ($mutt_attachment) =
1091 Locale::gettext::gettext("[-- Attachment #%d") =~
1093 ($mutt_contenttype) =
1094 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1097 Locale::gettext::gettext("[-- End of signed data --]\n") =~
1099 ($mutt_beginsigned) =
1100 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1102 ($mutt_pgpclearsigned) =
1103 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1105 ($mutt_pgpclearsigstart) =
1106 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1108 ($mutt_pgpencrypted) =
1109 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1111 ($mutt_pgpoutstart) =
1112 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1115 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1118 Locale::gettext::textdomain('gnupg');
1120 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1123 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1126 Locale::gettext::gettext("Can't check signature: %s\n") =~
1129 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1132 Locale::gettext::gettext('BAD signature from "%s"') =~
1135 Locale::gettext::gettext("Note: This key has expired!\n") =~
1138 Locale::gettext::gettext('Good signature from "%s"') =~
1139 m/^(([^"«%]*))["«%]/;
1141 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1147 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1149 if (($ml && $footers eq '')|| # no -l without -L
1150 ($ad && $ads eq '')|| # no -a without -A
1151 ($nohdr && $pgpshort)|| # --body and --pgp-short are like oil and water
1152 (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1153 ($ifile eq '')|| # no empty -i
1154 ($ofile eq '')) # no empty -o
1159 if (!open(IN, $ifile))
1160 { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1164 # First, check msg length and stop processing if msg is too long:
1165 if ((defined $maxlines) && (@message > $maxlines)) {
1166 if ($mua eq 'mutt') {
1169 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1172 splice(@message, $x, 0,
1173 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1175 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1177 if ($mua eq 'mutt') { exit(EX_OK); }
1181 # this should be self-explanatory:
1182 process_msg(\@message);
1184 # Finally, print clean lines:
1185 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1187 # vim600:set foldmethod=marker: