2 # $Id: t-prot,v 1.310 2010/07/10 19:14:36 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.100';
9 use constant REV => '';
10 use constant REL => q$Revision: 1.310 $=~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
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_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 --fixind fix quotes to adhere to RFC 3676
105 --ftr-ad enable aggressive ad footer matching; requires -A
106 --ftr-ml enable aggressive mailing list footer matching; req. -L
107 --groupwise delete Novell Groupwise style TOFU
108 -h, --help show this short help and exit
109 -i=INFILE file to be read; '-' for STDIN (default)
110 -k try to fix \"Kammquotes\"
111 --kdiff=n max. length difference between wrapped lines; req. -k
112 --kmaxl=n max. line length for wrapped line; requires -k
113 --kminl=n min. line length for wrapped line; requires -k
114 -L=DIRECTORY mailing list footer directory, treat mailing list
116 -l delete mailing list footer; requires -L
117 --lax-security use unsafe writing method; USE ON YOUR OWN RISK!
118 --locale=LOCALE internationalization; currently only used with -Mmutt
119 -M, --mua=MUA turn on special treatment for some mail user agents
120 -m delete MS style TOFU; careful: might be too aggressive
121 --max-lines=x maximum number of message lines
122 --ms-smart try to be smart with MS style TOFU; req. -Mmutt and -m
123 -o=OUTFILE file to be written to; '-' for STDOUT (default)
124 -P=MESSAGE user defined bounce message; requires -p
125 -p[=ADDRESS] redirect to ADDRESS if no TOFU was found
126 --pgp-move move pgp verification output to bottom; requires -Mmutt
127 --pgp-move-vrf move pgp output if verified and good; requires -Mmutt
128 --pgp-short hide non-relevant pgp key uids; requires -Mmutt
129 -r delete mail header lines
130 --reply squeeze multiple reply prefixes in subject line
131 -S[=n] suppress signatures with more than n lines (default $maxsig)
133 --sani sanitize some header fields
134 --sigsmax[=n] max number of sigs tolerated, no value for unlimited
135 --spass enable SpamAssassin workaround
136 -t delete traditional style TOFU
137 -v, --version show version string and exit
138 -w delete trailing whitespaces\n";
142 # version(): print version info and exit with appropriate exit code
144 print "$0 v".VER.REV." (Rev. ".REL."), Jochen Striepe <t-prot\@tolot.escape.de>
145 Get the latest version at <http://www.escape.de/users/tolot/mutt/>\n";
149 # sigint_handler(): what to do if we receive a single SIGINT
155 # Undoes MIME quoted-printable word encoding.
159 $word =~ s/=DF/ss/og;
160 $word =~ s/=C4/Ae/og;
161 $word =~ s/=D6/Oe/og;
162 $word =~ s/=DC/Ue/og;
163 $word =~ s/=E4/ae/og;
164 $word =~ s/=F6/oe/og;
165 $word =~ s/=FC/ue/og;
166 $word =~ s/=([0-9A-F]{2})/chr(hex $1)/ioge;
182 my $H = shift; # array of header lines
185 for ($i=0; $i<$#$H; $i++) {
186 if ($$H[$i] =~ /^subject: /io) {
188 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
189 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
192 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
196 while ($i<$#$H && $$H[$i] =~ /^\s/) {
198 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
199 $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
202 $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
207 elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
208 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
211 while ($i<$#$H && $$H[$i] =~ /^\s/) {
212 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
219 # remove_footers(): remove any trailing appearance of footers contained
220 # in the given directory.
222 my $L = shift; # array of message lines
223 my $S = shift; # array to store removed lines in
224 my $F = shift; # footers dir name
225 my $O = shift; # remove only one footer?
226 my $V = shift; # allow footers match before end of message
230 for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
233 if ($F && scalar(@$L)) {
234 if (!opendir(DIR, $F))
235 { print STDERR "Could not open $F: $!\n"; exit(EX_IOERR); }
236 my @feet = grep { /^[^.]/ && -f "$F/$_" } readdir DIR;
239 foreach my $f (@feet) {
240 if (!open(IN, "$F/$f"))
241 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
245 if (!scalar(@l)) { next; }
246 for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
249 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
250 if (scalar(@l)+$z<=scalar(@$L)) {
252 for(my $x=1; $x<=scalar(@l); $x++) {
253 if (index($$L[scalar(@$L)-$x-$z],
254 $l[scalar(@l)-$x])!=0) {
259 unshift(@$S, @$L[$#$L-$#l-$z..$#$L]);
260 splice(@$L, $#$L-$#l-$z);
261 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
262 unshift(@$S, pop(@$L));
264 if ($O) { last; } else { goto WIPE; }
270 while (scalar(@l)<=scalar(@$L)) {
271 for(my $x=1; $x<=scalar(@l); $x++) {
272 if (index($$L[scalar(@$L)-$x-$off], $l[scalar(@l)-$x])!=0) {
276 unshift(@$S, @$L[$#$L-$off-$#l..$#$L]);
277 splice(@$L, $#$L-$off-$#l);
278 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
279 unshift(@$S, pop(@$L));
289 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
292 my $L = shift; # array of message lines
293 my $V = shift; # array with verbatim list
294 my $max = 0; # plausible wraparound pos
296 # We scan the whole message first for a plausible common maximum line
297 # length where longer lines would be wrapped.
298 for (my $x=0; $x<$#$L; $x++) {
299 if ($$V[$x]!=1 && $max<length($$L[$x])) { $max = length($$L[$x]); }
302 # Next we see if there are plausible wraparounds.
303 for (my $x=0; $x+1<$#$L; $x++) {
306 # * it must not be verbatim,
307 # * the 2nd line must not be quoted nor empty nor just
308 # underlining some part of the line above (using '^')
309 # nor begin with a whitespace,
310 # * the 1st line must not end with a hyphen,
311 # * the 2nd line must not indicate content was deleted,
312 # * the 2nd line must not be some mutt(1) commentary,
313 # * there must not be a valid word wrap to produce a longer
314 # 1st line (if not quoted),
315 # * the 1st and 2nd line together must not be longer than some
316 # magical upper limit nor shorter than some magical lower
317 # bound nor nearly of the same length, and
318 # * the 3rd line must not be empty.
319 # With all that odds should be quite good that we have an
320 # automatedly wrapped line. Please send a note if you have
321 # additional good criteria. Thanks.
322 if (($$V[$x]!=1 && $$V[$x+1]!=1) &&
323 (index($$L[$x+1], $indent)!=0) &&
324 ($$L[$x+1] !~ /^$/) &&
325 ($$L[$x+1] !~ /^[\s^]/) &&
327 ($$L[$x+1] !~ /^\Q[...]\E\s*$/) &&
328 ($mua ne 'mutt' || $$L[$x+1] !~ /^(?:\e[^\a]+\a)?\[-- /) &&
329 (length($$L[$x])+index($$L[$x+1], ' ')>$max ||
330 (index($$L[$x+1], ' ')<0 &&
331 length($$L[$x])+length($$L[$x+1])>$max)) &&
332 (length($$L[$x])+length($$L[$x+1])<$kmaxl) &&
333 (length($$L[$x])+length($$L[$x+1])>$kminl) &&
334 (length($$L[$x])-length($$L[$x+1])>$kdiff) &&
335 ($$L[$x+2] !~ /^\s*$/))
337 $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
338 splice(@$L, $x+1, 1);
339 splice(@$V, $x+1, 1);
344 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
345 # but the last $x lines of them.
347 my $L = shift; # array of message lines
348 my $V = shift; # array with verbatim list
351 for (my $i=0; $i<=$#$L; $i++) {
358 if (index($$L[$i], $indent)==0) { $k++; } else {
363 $$L[$i] = "[---=| Quote block shrunk by $0: " .
364 "$x lines snipped |=---]\n";
366 splice(@$L, $i, $x-1);
367 splice(@$V, $i, $x-1);
376 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
381 my $L = shift; # message body
382 my $X = shift; # start line
383 my $Z = shift; # end line
388 if (index($$L[$X], "gpg: $gpg_WARNING")==0 ||
389 index($$L[$X], "gpg: $gpg_Warning")==0 ||
390 index($$L[$X], "gpg: $gpg_bad")==0 ||
391 index($$L[$X], "gpg: $gpg_Cantcheck")==0 ||
392 index($$L[$X], "gpg: $gpg_expired")==0 ||
393 index($$L[$X], "gpg: $gpg_bug")==0)
395 if (index($$L[$X], "gpg: $gpg_good")==0)
403 my $L = shift; # message body
404 my $V = shift; # verbatim list
405 my $H = shift; # headers
410 for (my $x=0; $x<scalar(@$L); $x++) {
411 if ($$V[$x]) { next; }
413 if ($$L[$x]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o)
416 for (my $m=0; $m<scalar(@$H); $m++) {
417 if (index($$H[$m], 'From:')==0) {
420 while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
421 { $from .= $$H[$m]; $m++; }
425 ($from) = $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
429 for (my $i=$x+1; $i<scalar(@$L); $i++) {
430 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
433 if ($uid>1 && index($$L[$i], $from)<0) {
439 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutend\E)/o) {
441 ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i)))
443 push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
444 splice(@$L, $x, $i-$x+2);
445 splice(@$V, $x, $i-$x+2);
446 $i -= $#{$tmp[$tmp]}-2;
456 (?:\Q$mutt_pgpencrypted\E |
457 \Q$mutt_pgpclearsigned\E|
461 splice(@$L, $x+1, 0, @{$tmp[$tmp]});
462 for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
463 splice(@$V, $x+1, 0, (0));
465 $x += scalar(@{$tmp[$tmp--]});
471 push(@$L, @{$tmp[$tmp--]});
473 for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
477 # write_msg(): output
482 if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
483 if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) {
484 print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
487 elsif (!open(OUT, $O)) {
488 print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
501 # process_msg(): This one proc does *everything* what has to be done with
502 # the lines of the message
506 my ($j, $x, $verb) = (0, 0, 0);
507 my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) =
508 ((), (), (), (), (), (), (), (), ());
510 # Remove and store lines we might need later...
513 for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
514 @hdr = @$lines[0..$x];
515 splice(@$lines, 0, $x+1);
517 # remember the original body lines count
518 my $linecount = scalar(@$lines);
520 # Sanitize header fields:
521 if ($reply || $sani) { sanitize_hdr(\@hdr); }
524 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
527 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
529 # See if we have a multipart content type. If yes, see if it is already
530 # ripped (e.g. by you MUA, assuming it does not get special treatment with
531 # --mua=foo), otherwise only leave the first part if it is plain text (if
532 # not, we are done - non-text messages are not our business).
533 if ($mua ne 'mutt') {
534 for ($x=0; $x<scalar(@hdr); $x++) {
535 if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
538 if ($foo =~ m!^multipart/!) {
541 if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
543 for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
544 if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) {
552 for (my $x=0; $x<scalar(@$lines); $x++) {
553 if (index($$lines[$x], '--'.$foo)!=0) { next; }
555 my $bar = 'text/plain';
556 for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
558 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) {
562 if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
564 if ($bar =~ m!^text/plain!) {
566 for ($z=1; $x+$z<@$lines; $z++) {
567 if (index($$lines[$x+$z], '--'.$foo)==0) {
571 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
573 @bo2 = @$lines[$x+$z..$#$lines];
574 splice(@$lines, $x+$z);
575 if ($$lines[$#$lines] =~ /^\s*$/) {
576 unshift(@bo2, pop @$lines);
578 @bo1 = @$lines[0..$x];
579 splice(@$lines, 0, $x+1);
581 # remove mailing list and ad footers within this
583 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
584 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
589 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
590 ($hdrs?undef:\@hdr), $lines);
604 for ($x=0; $x<scalar(@$lines); $x++) {
605 if ($$lines[$x] =~ /^\s*#v([+-])$/) {
606 $verb = $1 eq '+' ? 1 : 0;
608 } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/) {
609 # Detect and protect standard diffs.
610 # Skip and proceed to next line if premature file end or diff line
611 # numbers implausible (so it might be not a diff at all).
612 if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
614 for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
615 } elsif ($diff && $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/) {
616 if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
618 for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
619 } else { $vrb[$x] = $verb; }
622 # Calculate quoting ratio (with respect to verbatims):
623 if ($check && scalar(@$lines)) {
624 my ($y, $z) = (0, 0);
625 for ($x=0; $x<scalar(@$lines); $x++) {
628 if (index($$lines[$x], $indent)==0) { $y++; }
632 if ($y/$z>=$check_ratio) {
638 if ($mua eq 'mutt') {
639 # See if we find pgp output generated by mutt before we scramble
640 # the thing. If yes, see if we can beautify it.
641 if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
643 # Remove all but the first attachment (if this is text/plain)
644 # mutt did introduce (bah!). Remember, all this ugliness could
645 # be replaced with a proper and clean edit_filter patch in
647 for ($x=$#$lines; $x>=0; $x--) {
648 if ($vrb[$x]) { next; }
649 # The following regexp's are quite ugly because for most users
650 # these lines are coloured using termcap... (bah!)
652 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
657 (?:\Q$mutt_contenttype\E)
658 (?:text/plain|application/pgp)
662 (?:\Q$mutt_pgpsigned\E |
663 \Q$mutt_pgpclearsigned\E|
664 \Q$mutt_pgpencrypted\E)
667 # Strip attachments to prepare further processing
668 unshift(@att, @$lines[$x..$#$lines]);
670 # Try to fix trailing empty lines
671 while (scalar(@$lines) && $$lines[$#$lines] =~
672 /^(?:\e[^\a]+\a)?\s*$/) {
673 unshift(@att, pop(@$lines));
676 # Remove ML and ad footers within attachments:
678 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
679 if ($ad) { remove_footers($lines, \@tmp, $ads, undef); }
680 $x = scalar(@$lines);
684 # care about the rest
686 for ($x=0; $x<$#att; $x++) {
687 if ($vrb[scalar(@$lines)+$x]) { next; }
689 # Pipe message/rfc822 parts to another instance of
690 # process_msg() for further processing.
691 # Please note that we cannot see what a hierarchy the
692 # original message had -- if there were message/rfc822
693 # parts within other message/rfc822 parts constellations
694 # can occur which we cannot resolve. Therefore we simply
695 # do not even try to be smart. This should work for most
697 # The following regexp is quite ugly because for most
698 # users the line is coloured using termcap... (bah!)
700 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
703 (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
707 while ($att[$x] !~ /^\s*$/) { $x++; }
710 my @tmp = @att[$x..$#att];
712 splice(@att, $x, scalar(@att)-$x, @tmp);
720 if (scalar(@$lines)) {
725 for ($x = $#$lines; $x>=0; $x--) {
727 if ($$lines[$x] =~ /^-- $/) {
729 for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
730 if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
731 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
734 unshift(@sig, @$lines[$x..$#$lines]);
740 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
744 if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
745 if ($lsig && !$sig) {
746 unshift(@sig, "[---=| Overlong signature removed by $0: " .
747 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
752 unshift(@sig, @$lines[$x..$#$lines]);
755 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
757 # any trailing newlines?
758 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
759 elsif ($chk_empty) { $chk_empty = 0; }
764 # Fix quote markers to adhere to RFC 3676, this changes "> >" to ">> " (if
765 # the default $indent is used). The space after ">" is not mandatory by RFC
766 # but makes the result more readable.
768 for ($x=0; $x<scalar(@$lines); $x++) {
769 if (!$vrb[$x] && index($$lines[$x], $indent)==0) {
770 # We match space at the beginning to prevent removal of spaces
771 # directly after the last quote mark.
772 $$lines[$x] =~ /^((\Q$indent\E| )*\Q$indent\E ?)(.*)$/;
775 my $rest = $3; # if anything follows after the quote
777 $tmp .= ' ' if $rest; # don't create trailing whitespace
778 substr($$lines[$x], 0, $len, $tmp);
783 # See if there is some Kammquoting to fix:
784 if ($kamm) { decomb($lines, \@vrb); }
786 # Now care about TOFU.
787 # One common mispractice is M$ and Groupwise style TOFU:
789 # bloat this array if you want more internationalization:
790 my @tofu = ('Original Message',
791 'Original-Nachricht',
792 'Ursprüngliche Nachricht',
793 'Ursprüngliche Nachricht',
794 'Ursprungliche Nachricht',
796 'Ursprungligt meddelande',
797 'Oorspronkelijk bericht',
798 'Message d\'origine',
800 'Weitergeleitete Nachricht / Forwarded Message',
801 '[A-Za-z ]+ <(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})> schrieb:');
802 my $k = 0; # any text above?
803 my $tmp = 0; # flagged if inside PGP output
805 DONE: for ($x=0; $x<scalar(@$lines); $x++) {
807 foreach my $tmp (@tofu) {
808 if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
810 ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
811 $$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/))) {
819 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
822 # this line seems not necessary since mutt-1.5.18:
823 $$lines[$x] !~ /^\e[^\a]+\a$/o &&
825 /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
827 /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
828 ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
830 if ($mua eq 'mutt' && (!$tmp) &&
832 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
834 } elsif ($mua eq 'mutt' && $tmp &&
836 /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
838 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
847 # try to avoid false positives and only delete m$ style tofu if
848 # there is text above
850 if (!$ms_smart) { goto CLEAN; }
852 # first, see if there is pgp stuff inside the tofu:
853 my $p = 0; # levels of pgp signed parts
855 for (my $i=$x+1; $i<scalar(@$lines); $i++) {
857 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
862 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
863 if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
868 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
869 splice(@att, 0, $i+1);
877 # now removing is safe:
878 CLEAN: $j = scalar(@$lines)-$x;
883 # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
884 # much more aggressively, so we won't need to search any more if we
886 if ($trad && (!$j) && !$vrb[$#$lines]) {
887 if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) {
888 unshift(@sig, pop(@$lines));
894 for (my $i=$#$lines; $i>=0; $i--) {
895 if ($vrb[$i]) { last; }
896 if (index($$lines[$i], $indent)==0) {
900 elsif ($$lines[$i] !~ /^\s*$/) { last; }
904 # if there is no text above, we will assume the message is meant
905 # as forwarding and therefore OK
906 for (my $i=$k-1; $i>=0; $i--) {
907 if ($$lines[$i] !~ /^\s*$/o) {
920 # OK, if we found TOFU, we will leave a message that we were here...
922 # make sendmail bounce if we shall be picky
923 # and indeed found something:
929 eval { require Sys::Syslog; };
930 if ($@) { warn $@; } else {
931 Sys::Syslog::setlogsock('unix');
932 Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
933 Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
934 Sys::Syslog::closelog();
942 # if we were invoked just for checking and indeed found something,
943 # print out the error message and quit:
949 push(@$lines, "[---=| TOFU protection by $0: " .
950 "$j lines snipped |=---]\n");
952 elsif ($mda eq '1') { exit EX_OK; }
954 # Care for huge blocks of quoted original message:
955 if ($bigqn) { debigq($lines, \@vrb); }
957 # Care for trailing whitespaces:
959 for ($x=0; $x<scalar(@$lines); $x++) {
960 if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
964 # Care for punctuation abuse:
966 for ($x=0; $x<scalar(@$lines); $x++) {
967 if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
971 # (Nearly) at last care for multiple blank lines. (Do not do this
972 # earlier -- the way it is done right now would screw up the verbatim
975 # When handling regular TOFU above we move the last empty line from
976 # the body to the signature. This prevents the correct removal of
977 # empty lines before a signature (one line less is removed than it
978 # should) when -c and -t are used. This fixes it.
979 if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
980 push(@$lines, shift(@sig));
984 for ($x=scalar(@$lines)-1; $x>=0; $x--) {
986 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
987 $$lines[$x] =~ /^\s*$/o))
989 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
995 # Everything changing the body is done now. Time to fix the line count
996 # header so naive clients do not get confused. Just to be sure, append
997 # the old line count to X-headers.
998 my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
999 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) +
1000 (!$ad?scalar(@ads):0);
1001 if ($linecount-$l!=0) {
1002 for ($x=0; $x<scalar(@hdr); $x++) {
1004 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1006 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1012 # Finally, before leaving we put everything back in right order.
1013 unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1014 push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1020 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1021 ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1023 # command line switches
1024 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1025 $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1026 $sani, $sig, $sigint, $spass, $trad, $trsp) =
1027 (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);
1028 my $ifile = '-'; # use STDIN if nothing specified
1031 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1032 $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1034 # get command line params:
1035 if (!Getopt::Long::GetOptions(
1038 'bigq:s' => \$_t_bigq,
1039 'body' => \$_t_nohdr,
1041 'check:s' => \$_t_check,
1042 'debug|d' => \$sysl,
1045 'fixind' => \$fixind,
1046 'ftr-ad' => \$ftr_ad,
1047 'ftr-ml' => \$ftr_ml,
1048 'groupwise' => \$gw,
1049 'help|h' => \$_t_help,
1052 'kminl=i' => \$_t_kminl,
1053 'kmaxl=i' => \$_t_kmaxl,
1054 'kdiff=i' => \$_t_kdiff,
1057 'lax-security' => \$lax,
1058 'locale=s' => \$locale,
1059 'max-lines:i' => \$maxlines,
1060 'ms-smart' => \$ms_smart,
1061 'mua|M=s' => \$_t_mua,
1065 'p:s' => \$_t_redir,
1066 'pgp-short' => \$pgpshort,
1067 'pgp-move' => \$pgpmove,
1068 'pgp-move-vrf' => \$pgpmovevrf,
1071 'S:i' => \$_t_maxsig,
1074 'sigsmax:i' => \$_t_sigsmax,
1077 'version|v' => \$_t_ver,
1083 # clean up temp vals:
1084 if (defined $_t_bigq) {
1085 if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1088 if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1090 if (defined $_t_help) { help(); }
1091 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1092 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1093 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1094 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1095 if (defined $_t_mua) {
1097 # mutt still displays the message when ^C'ing pgp verification:
1098 if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1100 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1101 if (defined $_t_check) {
1103 while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1106 if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1109 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1110 if (defined $_t_ver) { version(); }
1111 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1112 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1113 if ($ms_smart) { $ms = 1; }
1115 if ($mua eq 'mutt') {
1116 if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1117 eval { require Locale::gettext; };
1118 if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1119 Locale::gettext::textdomain('mutt');
1120 ($mutt_attachment) =
1121 Locale::gettext::gettext("[-- Attachment #%d") =~
1123 ($mutt_contenttype) =
1124 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1127 Locale::gettext::gettext("[-- End of signed data --]\n") =~
1129 ($mutt_beginsigned) =
1130 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1132 ($mutt_pgpclearsigned) =
1133 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1135 ($mutt_pgpclearsigstart) =
1136 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1138 ($mutt_pgpencrypted) =
1139 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1141 ($mutt_pgpoutstart) =
1142 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1145 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1148 Locale::gettext::textdomain('gnupg');
1150 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1153 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1156 Locale::gettext::gettext("Can't check signature: %s\n") =~
1159 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1162 Locale::gettext::gettext('BAD signature from "%s"') =~
1165 Locale::gettext::gettext("Note: This key has expired!\n") =~
1168 Locale::gettext::gettext('Good signature from "%s"') =~
1169 m/^(([^"«%]*))["«%]/;
1171 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1177 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1179 if (($ml && $footers eq '')|| # no -l without -L
1180 ($ad && $ads eq '')|| # no -a without -A
1181 ($nohdr && $pgpshort)|| # --body and --pgp-short are like oil and water
1182 (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1183 ($ifile eq '')|| # no empty -i
1184 ($ofile eq '')) # no empty -o
1189 if (!open(IN, $ifile))
1190 { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1194 # First, check msg length and stop processing if msg is too long:
1195 if ((defined $maxlines) && (@message > $maxlines)) {
1196 if ($mua eq 'mutt') {
1199 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1202 splice(@message, $x, 0,
1203 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1205 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1207 if ($mua eq 'mutt') { exit(EX_OK); }
1211 # this should be self-explanatory:
1212 process_msg(\@message);
1214 # Finally, print clean lines:
1215 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1217 # vim600:set foldmethod=marker: