2 # $Id: t-prot,v 1.285 2010/02/15 20:13:00 jochen Exp $
6 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
7 use Getopt::Mixed qw(nextOption);
8 use constant VER => '2.15';
9 use constant REV => '';
10 use constant REL => q$Revision: 1.285 $=~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 $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 $indent = '>'; # Indent string, regexp to identify a quoted line
46 $kminl = 65; # see decomb() for details
49 $pgpshort = 0; # hide pgp key ids if set
50 $pgpmove = 0; # move pgp output to bottom if set
52 $sign = 1; # max number of sigs tolerated, undef for no limit
53 $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";
54 $ftr_ad = undef; # too hard on performance to be default
55 $ftr_ml = undef; # too hard on performance to be default
56 $ofile = '-'; # use STDOUT if nothing is specified
57 $spass_prefix = 'SPAM: ';
58 $check_ratio = 0; # off by default
59 $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";
60 $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";
61 # end of user adjusted vals
64 # set the defaults to the C locale
65 $mutt_attachment = '[-- Attachment #';
66 $mutt_contenttype = '[-- Type: ';
67 $mutt_pgpsigned = '[-- End of signed data --]';
68 $mutt_beginsigned = '[-- The following data is signed --]';
69 $mutt_pgpclearsigstart = '[-- BEGIN PGP SIGNED MESSAGE --]';
70 $mutt_pgpclearsigned = '[-- END PGP SIGNED MESSAGE --]';
71 $mutt_pgpencrypted = '[-- End of PGP/MIME encrypted data --]';
72 $mutt_pgpoutstart = '[-- PGP output follows (current time:';
73 $mutt_pgpoutend = '[-- End of PGP output --]';
75 # set the defaults to the C locale
76 $gpg_WARNING = 'WARNING: ';
77 # (yes, the translations in gnupg's po files *are* braindamaged):
78 $gpg_Warning = 'WARNING: ';
79 $gpg_Cantcheck = 'Can\'t check signature: ';
81 $gpg_bad = 'BAD signature from ';
82 $gpg_expired = 'Note: This key has expired!';
83 $gpg_good = 'Good signature from';
84 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
85 $gpg_bug = '... this is a bug (';
88 # help(): print help text and exit with appropriate exit code
90 print "Usage: $0 [options]
91 -A=DIRECTORY ad footer directory, treat ad footers as signature
92 -a remove ad footers; requires -A
93 --bigq[=n[,x]] remove all but x lines of quotes with more than n
94 lines; default is n=30 and x=10
95 --body input has no headers; does not work with --pgp-short;
96 multipart messages will not be detected
97 -c merge multiple blank lines
98 --check[=FLAGS] check various criteria, print error message and quit;
99 see man page for details
100 -d, --debug print notice to syslog when bouncing; requires -p
101 --diff tolerate diffs appended *after* the signature
102 -e force ellipsis for excessive punctuation
103 --ftr-ad enable aggressive ad footer matching; requires -A
104 --ftr-ml enable aggressive mailing list footer matching; req. -L
105 --groupwise delete Novell Groupwise style TOFU
106 -h, --help show this short help and exit
107 -i=INFILE file to be read; '-' for STDIN (default)
108 -k try to fix \"Kammquotes\"
109 --kminl=n min. line length for wrapped line; requires -k
110 --kmaxl=n max. line length for wrapped line; requires -k
111 --kdiff=n max. length difference between wrapped lines; req. -k
112 -L=DIRECTORY mailing list footer directory, treat mailing list
114 -l delete mailing list footer; requires -L
115 --lax-security use unsafe writing method; USE ON YOUR OWN RISK!
116 --locale=LOCALE internationalization; currently only used with -Mmutt
117 -M, --mua=MUA turn on special treatment for some mail user agents
118 -m delete MS style TOFU; careful: might be too agressive
119 --max-lines=x maximum number of message lines
120 --ms-smart try to be smart with MS style TOFU; req. -Mmutt and -m
121 -o=OUTFILE file to be written to; '-' for STDOUT (default), 'NONE'
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 shrinked 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 '-')) {
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 } else { $vrb[$x] = $verb; }
610 # Calculate quoting ratio (with respect to verbatims):
611 if ($check && scalar(@$lines)) {
612 my ($y, $z) = (0, 0);
613 for ($x=0; $x<scalar(@$lines); $x++) {
616 if (index($$lines[$x], $indent)==0) { $y++; }
620 if ($y/$z>=$check_ratio) {
626 if ($mua eq 'mutt') {
627 # See if we find pgp output generated by mutt before we scramble
628 # the thing. If yes, see if we can beautify it.
629 if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
631 # Remove all but the first attachment (if this is text/plain)
632 # mutt did introduce (bah!). Remember, all this ugliness could
633 # be replaced with a proper and clean edit_filter patch in
635 for ($x=$#$lines; $x>=0; $x--) {
636 if ($vrb[$x]) { next; }
637 # The following regexp's are quite ugly because for most users
638 # these lines are coloured using termcap... (bah!)
640 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
645 (?:\Q$mutt_contenttype\E)
646 (?:text/plain|application/pgp)
650 (?:\Q$mutt_pgpsigned\E |
651 \Q$mutt_pgpclearsigned\E|
652 \Q$mutt_pgpencrypted\E)
655 # Strip attachments to prepare further processing
656 unshift(@att, @$lines[$x..$#$lines]);
658 # Try to fix trailing empty lines
659 while (scalar(@$lines) && $$lines[$#$lines] =~
660 /^(?:\e[^\a]+\a)?\s*$/) {
661 unshift(@att, pop(@$lines));
664 # Remove ML and ad footers within attachments:
666 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
667 if ($ad) { remove_footers($lines, \@tmp, $ads, undef); }
668 $x = scalar(@$lines);
672 # care about the rest
674 for ($x=0; $x<$#att; $x++) {
675 if ($vrb[scalar(@$lines)+$x]) { next; }
677 # Pipe message/rfc822 parts to another instance of
678 # process_msg() for further processing.
679 # Please note that we cannot see what a hierarchy the
680 # original message had -- if there were message/rfc822
681 # parts within other message/rfc822 parts constellations
682 # can occur which we cannot resolve. Therefore we simply
683 # do not even try to be smart. This should work for most
685 # The following regexp is quite ugly because for most
686 # users the line is coloured using termcap... (bah!)
688 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
691 (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
695 while ($att[$x] !~ /^\s*$/) { $x++; }
698 my @tmp = @att[$x..$#att];
700 splice(@att, $x, scalar(@att)-$x, @tmp);
708 if (scalar(@$lines)) {
713 for ($x = $#$lines; $x>=0; $x--) {
715 if ($$lines[$x] =~ /^-- $/) {
717 for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
718 if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
719 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
722 unshift(@sig, @$lines[$x..$#$lines]);
728 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
732 if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
733 if ($lsig && !$sig) {
734 unshift(@sig, "[---=| Overlong signature removed by $0: " .
735 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
740 unshift(@sig, @$lines[$x..$#$lines]);
743 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
745 # any trailing newlines?
746 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
747 elsif ($chk_empty) { $chk_empty = 0; }
752 # See if there is some Kammquoting to fix:
753 if ($kamm) { decomb($lines, \@vrb); }
755 # Now care about TOFU.
756 # One common mispractice is M$ and Groupwise style TOFU:
758 # bloat this array if you want more internationalization:
759 my @tofu = ('Original Message',
760 'Original-Nachricht',
761 'Ursprüngliche Nachricht',
762 'Ursprüngliche Nachricht',
763 'Ursprungliche Nachricht',
765 'Ursprungligt meddelande',
766 'Oorspronkelijk bericht',
767 'Message d\'origine',
769 'Weitergeleitete Nachricht / Forwarded Message');
770 my $k = 0; # any text above?
771 my $tmp = 0; # flagged if inside PGP output
773 DONE: for ($x=0; $x<scalar(@$lines); $x++) {
775 foreach my $tmp (@tofu) {
776 if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
778 ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
779 $$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/))) {
787 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
790 # this line seems not necessary since mutt-1.5.18:
791 $$lines[$x] !~ /^\e[^\a]+\a$/o &&
793 /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
795 /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
796 ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
798 if ($mua eq 'mutt' && (!$tmp) &&
800 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
802 } elsif ($mua eq 'mutt' && $tmp &&
804 /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
806 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
815 # try to avoid false positives and only delete m$ style tofu if
816 # there is text above
818 if (!$ms_smart) { goto CLEAN; }
820 # first, see if there is pgp stuff inside the tofu:
821 my $p = 0; # levels of pgp signed parts
823 for (my $i=$x+1; $i<scalar(@$lines); $i++) {
825 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
830 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
831 if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
836 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
837 splice(@att, 0, $i+1);
845 # now removing is safe:
846 CLEAN: $j = scalar(@$lines)-$x;
851 # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
852 # much more aggressively, so we won't need to search any more if we
854 if ($trad && (!$j) && !$vrb[$#$lines]) {
855 if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) {
856 unshift(@sig, pop(@$lines));
862 for (my $i=$#$lines; $i>=0; $i--) {
863 if ($$lines[$i] =~ /^$indent/o) {
867 elsif ($$lines[$i] !~ /^\s*$/) { last; }
871 # if there is no text above, we will assume the message is meant
872 # as forwarding and therefore OK
873 for (my $i=$k-1; $i>=0; $i--) {
874 if ($$lines[$i] !~ /^\s*$/o) {
887 # OK, if we found TOFU, we will leave a message that we were here...
889 # make sendmail bounce if we shall be picky
890 # and indeed found something:
896 eval { require Sys::Syslog; };
897 if ($@) { warn $@; } else {
898 Sys::Syslog::setlogsock('unix');
899 Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
900 Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
901 Sys::Syslog::closelog();
909 # if we were invoked just for checking and indeed found something,
910 # print out the error message and quit:
916 push(@$lines, "[---=| TOFU protection by $0: " .
917 "$j lines snipped |=---]\n");
919 elsif ($mda eq '1') { exit EX_OK; }
921 # Care for huge blocks of quoted original message:
922 if ($bigqn) { debigq($lines, \@vrb); }
924 # Care for trailing whitespaces:
926 for ($x=0; $x<scalar(@$lines); $x++) {
927 if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
931 # Care for punctuation abuse:
933 for ($x=0; $x<scalar(@$lines); $x++) {
934 if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
938 # (Nearly) at last care for multiple blank lines. (Do not do this
939 # earlier -- the way it is done right now would screw up the verbatim
943 for ($x=scalar(@$lines)-1; $x>=0; $x--) {
945 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
946 $$lines[$x] =~ /^\s*$/o))
948 if ($t<2) { $t++; } else { splice(@$lines, $x, 1); }
954 # Everything changing the body is done now. Time to fix the line count
955 # header so naive clients do not get confused. Just to be sure, append
956 # the old line count to X-headers.
957 my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
958 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) +
959 (!$ad?scalar(@ads):0);
960 if ($linecount-$l!=0) {
961 for ($x=0; $x<scalar(@hdr); $x++) {
963 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
965 $hdr[$#hdr] = "X-Old-Lines: $2\n";
971 # Finally, before leaving we put everything back in right order.
972 unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
973 push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
979 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
980 ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
982 # command line switches
983 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $footers, $lax,
984 $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
985 $sani, $sig, $sigint, $spass, $trad, $trsp) =
986 (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);
987 my $ifile = '-'; # use STDIN if nothing specified
989 # get command line params:
990 Getopt::Mixed::init('a A=s c d e h i=s k L=s l m M=s o=s P=s p:s r S:i'.
991 ' s t v w bigq:s body check:s debug>d diff ftr-ad ftr-ml groupwise'.
992 ' help>h kminl=i kmaxl=i kdiff=i lax-security locale=s max-lines=i'.
993 ' ms-smart mua>M pgp-short pgp-move pgp-move-vrf reply sani sigsmax:i'.
995 while (my ($opt, $val, $pretty) = nextOption()) {
996 if ($opt eq 'a') { $ad = 1; }
997 elsif ($opt eq 'A') { $ads = $val; }
998 elsif ($opt eq 'bigq') {
999 if ($val !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1002 if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1004 elsif ($opt eq 'body') { $nohdr = 1; $hdrs = 1; }
1005 elsif ($opt eq 'c') { $cr = 1; }
1006 elsif ($opt eq 'check') {
1008 while ($val && $val =~ /^([^,\s]+)(?:,(\S+))?$/) {
1012 if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) {
1013 $check_ratio = $1?$1:0.75;
1017 elsif ($opt eq 'd') { $sysl = 1; }
1018 elsif ($opt eq 'diff') { $diff = 1; }
1019 elsif ($opt eq 'e') { $elli = 1; }
1020 elsif ($opt eq 'ftr-ad') { $ftr_ad = 1; $ad = 1; }
1021 elsif ($opt eq 'ftr-ml') { $ftr_ml = 1; $ml = 1; }
1022 elsif ($opt eq 'groupwise') { $gw = 1; }
1023 elsif ($opt eq 'i') { $ifile = $val; }
1024 elsif ($opt eq 'k') { $kamm = 1; }
1025 elsif ($opt eq 'kminl') { $kminl = $val; $kamm = 1; }
1026 elsif ($opt eq 'kmaxl') { $kmaxl = $val; $kamm = 1; }
1027 elsif ($opt eq 'kdiff') { $kdiff = $val; $kamm = 1; }
1028 elsif ($opt eq 'L') { $footers = $val; }
1029 elsif ($opt eq 'l') { $ml = 1; }
1030 elsif ($opt eq 'lax-security') { $lax = 1; }
1031 elsif ($opt eq 'locale') { $locale = $val; }
1032 elsif ($opt eq 'm') { $ms = 1; }
1033 elsif ($opt eq 'max-lines') { $maxlines = $val; }
1034 elsif ($opt eq 'ms-smart') { $ms_smart = 1; $ms = 1; }
1035 elsif ($opt eq 'M') {
1038 if ($mua eq 'mutt') {
1039 # mutt still displays the message when ^C'ing pgp verification:
1040 $SIG{'INT'} = 'sigint_handler';
1043 elsif ($opt eq 'o') { $ofile = $val; }
1044 elsif ($opt eq 'P') { $boun = $val; }
1045 elsif ($opt eq 'p') { $mda = $val ? $val : '1'; }
1046 elsif ($opt eq 'pgp-short') { $pgpshort = 1; }
1047 elsif ($opt eq 'pgp-move') { $pgpmove = 1; }
1048 elsif ($opt eq 'pgp-move-vrf') { $pgpmovevrf = 1; }
1049 elsif ($opt eq 'r') { $hdrs = 1; }
1050 elsif ($opt eq 'reply') { $reply = 1; }
1051 elsif ($opt eq 'S') { $lsig = $val ? $val : $maxsig; }
1052 elsif ($opt eq 's') { $sig = 1; }
1053 elsif ($opt eq 'sani') { $sani = 1; }
1054 elsif ($opt eq 'sigsmax') { $sign = $val ? $val : undef; }
1055 elsif ($opt eq 'spass') { $spass = 1; }
1056 elsif ($opt eq 't') { $trad = 1; }
1057 elsif ($opt eq 'v') { version(); }
1058 elsif ($opt eq 'w') { $trsp = 1; }
1061 Getopt::Mixed::cleanup();
1062 if (($ml && $footers eq '')|| # no -l without -L
1063 ($ad && $ads eq '')|| # no -a without -A
1064 ($nohdr && $pgpshort)|| # --body and --pgp-short are oil and water
1065 (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitazing without headers :)
1066 ($ifile eq '')|| # no empty -i
1067 ($ofile eq '')) # no empty -o
1071 if ($mua eq 'mutt') {
1072 if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1073 eval { require Locale::gettext; };
1074 if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1075 Locale::gettext::textdomain('mutt');
1076 ($mutt_attachment) =
1077 Locale::gettext::gettext("[-- Attachment #%d") =~
1079 ($mutt_contenttype) =
1080 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1083 Locale::gettext::gettext("[-- End of signed data --]\n") =~
1085 ($mutt_beginsigned) =
1086 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1088 ($mutt_pgpclearsigned) =
1089 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1091 ($mutt_pgpclearsigstart) =
1092 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1094 ($mutt_pgpencrypted) =
1095 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1097 ($mutt_pgpoutstart) =
1098 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1101 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1104 Locale::gettext::textdomain('gnupg');
1106 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1109 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1112 Locale::gettext::gettext("Can't check signature: %s\n") =~
1115 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1118 Locale::gettext::gettext('BAD signature from "%s"') =~
1121 Locale::gettext::gettext("Note: This key has expired!\n") =~
1124 Locale::gettext::gettext('Good signature from "%s"') =~
1125 m/^(([^"«%]*))["«%]/;
1127 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1133 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1137 if (!open(IN, $ifile))
1138 { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1142 # First, check msg length and stop processing if msg is too long:
1143 if ((defined $maxlines) && (@message > $maxlines)) {
1144 if ($ofile ne 'NONE') {
1145 if ($mua eq 'mutt') {
1148 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1151 splice(@message, $x, 0,
1152 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1155 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1157 if ($mua eq 'mutt') { exit(EX_OK); }
1161 # this should be self-explanatory:
1162 process_msg(\@message);
1164 # Finally, print clean lines:
1165 if ($ofile ne 'NONE') {
1166 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1169 # vim600:set foldmethod=marker: