2 # $Id: t-prot,v 1.330 2015/03/26 08:59: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 => '3.4';
9 use constant REV => '';
10 use constant REL => q$Revision: 1.330 $=~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
36 $mutt_ssloutstart $mutt_ssloutend
40 # Please adjust these vals to your needs only if you know what you are
41 # doing. They are no constants because command line can change them or
42 # they are used in some rexexp.
44 $maxsig = 4; # max. valid signature length
45 $maxlines = undef; # no limit of message lines
46 $crshrink = 2; # multiple blank lines are shrunk to $crshrink lines
47 $indent = '>'; # Indent string to identify a quoted line
48 $kminl = 65; # see decomb() for details
51 $pgpshort = 0; # hide pgp key ids if set
52 $pgpmove = 0; # move pgp output to bottom if set
54 $sign = 1; # max number of sigs tolerated, undef for no limit
55 $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";
56 $ftr_ad = undef; # too hard on performance to be default
57 $ftr_ml = undef; # too hard on performance to be default
58 $ofile = '-'; # use STDOUT if nothing is specified
59 $spass_prefix = 'SPAM: ';
60 $check_ratio = 0; # off by default
61 $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";
62 $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";
63 # end of user adjusted vals
66 # set the defaults to the C locale
67 $mutt_attachment = '[-- Attachment #';
68 $mutt_contenttype = '[-- Type: ';
69 $mutt_pgpsigned = '[-- End of signed data --]';
70 $mutt_beginsigned = '[-- The following data is signed --]';
71 $mutt_pgpclearsigstart = '[-- BEGIN PGP SIGNED MESSAGE --]';
72 $mutt_pgpcryptend = '[-- END PGP MESSAGE --]';
73 $mutt_pgpcryptstart = '[-- BEGIN PGP MESSAGE --]';
74 $mutt_pgpclearsigned = '[-- END PGP SIGNED MESSAGE --]';
75 $mutt_pgpencrypted = '[-- End of PGP/MIME encrypted data --]';
76 $mutt_pgpoutstart = '[-- PGP output follows (current time:';
77 $mutt_pgpoutend = '[-- End of PGP output --]';
78 $mutt_ssloutstart = '[-- OpenSSL output follows';
79 $mutt_ssloutend = '[-- End of OpenSSL output --]';
81 # set the defaults to the C locale
82 $gpg_WARNING = 'WARNING: ';
83 # (yes, the translations in gnupg's po files *are* braindamaged):
84 $gpg_Warning = 'WARNING: ';
85 $gpg_Cantcheck = 'Can\'t check signature: ';
87 $gpg_bad = 'BAD signature from ';
88 $gpg_expired = 'Note: This key has expired!';
89 $gpg_good = 'Good signature from';
90 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
91 $gpg_bug = '... this is a bug (';
94 # help(): print help text and exit with appropriate exit code
96 print "Usage: $0 [options]
97 -A DIRECTORY ad footer directory, treat ad footers as signature
98 -a remove ad footers; requires -A
99 --bigq[=n[,x]] remove all but x lines of quotes with more than n
100 lines; default is n=30 and x=10
101 --body input has no headers; does not work with --pgp-short;
102 multipart messages will not be detected
103 -c[n] merge multiple blank lines to n lines (default is 2)
104 --check[=FLAGS] check various criteria, print error message and quit;
105 see man page for details
106 -d, --debug print notice to syslog when bouncing; requires -p
107 --diff tolerate diffs
108 -e force ellipsis for excessive punctuation
109 --fixind fix quotes to adhere to RFC 3676
110 --ftr-ad enable aggressive ad footer matching; requires -A
111 --ftr-ml enable aggressive mailing list footer matching; req. -L
112 --groupwise delete Novell Groupwise style TOFU
113 -h, --help show this short help and exit
114 -i INFILE file to be read; '-' for STDIN (default)
115 -k try to fix \"Kammquotes\"
116 --kdiff=n max. length difference between wrapped lines; req. -k
117 --kmaxl=n max. line length for wrapped line; requires -k
118 --kminl=n min. line length for wrapped line; requires -k
119 -L DIRECTORY mailing list footer directory, treat mailing list
121 -l delete mailing list footer; requires -L
122 --lax-security use unsafe writing method; USE ON YOUR OWN RISK!
123 --locale=LOCALE internationalization; currently only used with -Mmutt
124 -M, --mua=MUA turn on special treatment for some mail user agents
125 -m delete MS style TOFU; careful: might be too aggressive
126 --max-lines=x maximum number of message lines
127 --ms-smart try to be smart with MS style TOFU; req. -Mmutt and -m
128 -o OUTFILE file to be written to; '-' for STDOUT (default)
129 -P MESSAGE user defined bounce message; requires -p
130 -p [ADDRESS] redirect to ADDRESS if no TOFU was found
131 --pgp-move move pgp verification output to bottom; requires -Mmutt
132 --pgp-move-vrf move pgp output if verified and good; requires -Mmutt
133 --pgp-short hide non-relevant pgp key uids; requires -Mmutt
134 -r delete mail header lines
135 --reply squeeze multiple reply prefixes in subject line
136 -S[n] suppress signatures with more than n lines (default $maxsig)
138 --sani sanitize some header fields
139 --sigsmax[=n] max number of sigs tolerated, no value for unlimited
140 --spass enable SpamAssassin workaround
141 -t delete traditional style TOFU
142 -v, --version show version string and exit
143 -w delete trailing whitespaces\n";
147 # version(): print version info and exit with appropriate exit code
149 print "$0 v".VER.REV." (Rev. ".REL."), Jochen Striepe <t-prot\@tolot.escape.de>
150 Get the latest version at <http://www.escape.de/users/tolot/mutt/>\n";
154 # sigint_handler(): what to do if we receive a single SIGINT
160 # Undoes MIME quoted-printable word encoding.
164 $word =~ s/=DF/ss/og;
165 $word =~ s/=C4/Ae/og;
166 $word =~ s/=D6/Oe/og;
167 $word =~ s/=DC/Ue/og;
168 $word =~ s/=E4/ae/og;
169 $word =~ s/=F6/oe/og;
170 $word =~ s/=FC/ue/og;
171 $word =~ s/=([0-9A-F]{2})/chr(hex $1)/ioge;
187 my $H = shift; # array of header lines
190 for ($i=0; $i<$#$H; $i++) {
191 if ($$H[$i] =~ /^subject: /io) {
193 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
194 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
197 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
201 while ($i<$#$H && $$H[$i] =~ /^\s/) {
203 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
204 $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
207 $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
212 elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
213 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
216 while ($i<$#$H && $$H[$i] =~ /^\s/) {
217 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
224 # remove_footers(): remove any trailing appearance of footers contained
225 # in the given directory.
227 my $L = shift; # array of message lines
228 my $S = shift; # array to store removed lines in
229 my $F = shift; # footers dir name
230 my $O = shift; # remove only one footer?
231 my $V = shift; # allow footers match before end of message
235 for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
238 if ($F && scalar(@$L)) {
239 if (!opendir(DIR, $F))
240 { print STDERR "Could not open $F: $!\n"; exit(EX_IOERR); }
241 my @feet = grep { /^[^.]/ && -f "$F/$_" } readdir DIR;
244 foreach my $f (@feet) {
245 if (!open(IN, "$F/$f"))
246 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
250 if (!scalar(@l)) { next; }
251 for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
254 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
255 if (scalar(@l)+$z<=scalar(@$L)) {
257 for(my $x=1; $x<=scalar(@l); $x++) {
258 if (index($$L[scalar(@$L)-$x-$z],
259 $l[scalar(@l)-$x])!=0) {
264 unshift(@$S, @$L[$#$L-$#l-$z..$#$L]);
265 splice(@$L, $#$L-$#l-$z);
266 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
267 unshift(@$S, pop(@$L));
269 if ($O) { last; } else { goto WIPE; }
275 while (scalar(@l)<=scalar(@$L)) {
276 for(my $x=1; $x<=scalar(@l); $x++) {
277 if (index($$L[scalar(@$L)-$x-$off], $l[scalar(@l)-$x])!=0) {
281 unshift(@$S, @$L[$#$L-$off-$#l..$#$L]);
282 splice(@$L, $#$L-$off-$#l);
283 while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
284 unshift(@$S, pop(@$L));
294 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
297 my $L = shift; # array of message lines
298 my $V = shift; # array with verbatim list
299 my $max = 0; # plausible wraparound pos
301 # We scan the whole message first for a plausible common maximum line
302 # length where longer lines would be wrapped.
303 for (my $x=0; $x<$#$L; $x++) {
304 if ($$V[$x]!=1 && $max<length($$L[$x])) { $max = length($$L[$x]); }
307 # Next we see if there are plausible wraparounds.
308 for (my $x=0; $x+1<$#$L; $x++) {
311 # * it must not be verbatim,
312 # * the 2nd line must not be quoted nor empty nor just
313 # underlining some part of the line above (using '^')
314 # nor begin with a whitespace,
315 # * the 1st line must not end with a hyphen,
316 # * the 2nd line must not indicate content was deleted,
317 # * the 2nd line must not be some mutt(1) commentary,
318 # * there must not be a valid word wrap to produce a longer
319 # 1st line (if not quoted),
320 # * the 1st and 2nd line together must not be longer than some
321 # magical upper limit nor shorter than some magical lower
322 # bound nor nearly of the same length, and
323 # * the 3rd line must not be empty.
324 # With all that odds should be quite good that we have an
325 # automatedly wrapped line. Please send a note if you have
326 # additional good criteria. Thanks.
327 if (($$V[$x]!=1 && $$V[$x+1]!=1) &&
328 (index($$L[$x+1], $indent)!=0) &&
329 ($$L[$x+1] !~ /^$/) &&
330 ($$L[$x+1] !~ /^[\s^]/) &&
332 ($$L[$x+1] !~ /^\Q[...]\E\s*$/) &&
333 ($mua ne 'mutt' || $$L[$x+1] !~ /^(?:\e[^\a]+\a)?\[-- /) &&
334 (length($$L[$x])+index($$L[$x+1], ' ')>$max ||
335 (index($$L[$x+1], ' ')<0 &&
336 length($$L[$x])+length($$L[$x+1])>$max)) &&
337 (length($$L[$x])+length($$L[$x+1])<$kmaxl) &&
338 (length($$L[$x])+length($$L[$x+1])>$kminl) &&
339 (length($$L[$x])-length($$L[$x+1])>$kdiff) &&
340 ($$L[$x+2] !~ /^\s*$/))
342 $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
343 splice(@$L, $x+1, 1);
344 splice(@$V, $x+1, 1);
349 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
350 # but the last $x lines of them.
352 my $L = shift; # array of message lines
353 my $V = shift; # array with verbatim list
356 for (my $i=0; $i<=$#$L; $i++) {
363 if (index($$L[$i], $indent)==0) { $k++; } else {
368 $$L[$i] = "[---=| Quote block shrunk by $0: " .
369 "$x lines snipped |=---]\n";
371 splice(@$L, $i, $x-1);
372 splice(@$V, $i, $x-1);
381 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
386 my $L = shift; # message body
387 my $X = shift; # start line
388 my $Z = shift; # end line
393 if (index($$L[$X], "gpg: $gpg_WARNING")==0 ||
394 index($$L[$X], "gpg: $gpg_Warning")==0 ||
395 index($$L[$X], "gpg: $gpg_bad")==0 ||
396 index($$L[$X], "gpg: $gpg_Cantcheck")==0 ||
397 index($$L[$X], "gpg: $gpg_expired")==0 ||
398 index($$L[$X], "gpg: $gpg_bug")==0)
400 if (index($$L[$X], "gpg: $gpg_good")==0)
408 my $L = shift; # message body
409 my $V = shift; # verbatim list
410 my $H = shift; # headers
415 for (my $x=0; $x<scalar(@$L); $x++) {
416 if ($$V[$x]) { next; }
418 if ($$L[$x]=~/^(?:\e[^\a]+\a)?
419 (?:\Q$mutt_pgpoutstart\E|\Q$mutt_ssloutstart\E)/ox) {
422 for (my $m=0; $m<scalar(@$H); $m++) {
423 if (index($$H[$m], 'From:')==0) {
426 while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
427 { $from .= $$H[$m]; $m++; }
431 if (defined($from)) {
433 $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
439 for (my $i=$x+1; $i<scalar(@$L); $i++) {
440 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
443 if (!defined($from) ||
444 ($uid>1 && index($$L[$i], $from)<0)) {
450 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?
451 (?:\Q$mutt_pgpoutend\E|\Q$mutt_ssloutend\E)/ox) {
453 ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i))) {
454 push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
455 splice(@$L, $x, $i-$x+2);
456 splice(@$V, $x, $i-$x+2);
457 $i -= $#{$tmp[$tmp]}-2;
467 (?:\Q$mutt_pgpencrypted\E|
468 \Q$mutt_pgpcryptend\E|
469 \Q$mutt_pgpclearsigned\E|
472 splice(@$L, $x+1, 0, @{$tmp[$tmp]});
473 for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
474 splice(@$V, $x+1, 0, (0));
476 $x += scalar(@{$tmp[$tmp--]});
482 push(@$L, @{$tmp[$tmp--]});
484 for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
488 # write_msg(): output
493 if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
494 if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) {
495 print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
498 elsif (!open(OUT, $O)) {
499 print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
512 # process_msg(): This one proc does *everything* what has to be done with
513 # the lines of the message
517 my ($j, $x, $verb) = (0, 0, 0);
518 my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) =
519 ((), (), (), (), (), (), (), (), ());
521 # Remove and store lines we might need later...
524 for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
525 @hdr = @$lines[0..$x];
526 splice(@$lines, 0, $x+1);
528 # remember the original body lines count
529 my $linecount = scalar(@$lines);
531 # Sanitize header fields:
532 if ($reply || $sani) { sanitize_hdr(\@hdr); }
535 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
538 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
540 # See if we have a multipart content type. If yes, see if it is already
541 # ripped (e.g. by you MUA, assuming it does not get special treatment with
542 # --mua=foo), otherwise only leave the first part if it is plain text (if
543 # not, we are done - non-text messages are not our business).
544 if ($mua ne 'mutt') {
545 for ($x=0; $x<scalar(@hdr); $x++) {
546 if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
549 if ($foo =~ m!^multipart/!) {
552 if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
554 for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
555 if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) {
563 for (my $x=0; $x<scalar(@$lines); $x++) {
564 if (index($$lines[$x], '--'.$foo)!=0) { next; }
566 my $bar = 'text/plain';
567 for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
569 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) {
573 if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
575 if ($bar =~ m!^text/plain!i) {
577 for ($z=1; $x+$z<@$lines; $z++) {
578 if (index($$lines[$x+$z], '--'.$foo)==0) {
582 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
584 @bo2 = @$lines[$x+$z..$#$lines];
585 splice(@$lines, $x+$z);
586 if ($$lines[$#$lines] =~ /^\s*$/) {
587 unshift(@bo2, pop @$lines);
589 @bo1 = @$lines[0..$x];
590 splice(@$lines, 0, $x+1);
592 # remove mailing list and ad footers within this
594 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
595 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
600 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
601 ($hdrs?undef:\@hdr), $lines);
615 for ($x=0; $x<scalar(@$lines); $x++) {
616 if ($$lines[$x] =~ /^\s*#v([+-])$/o) {
617 $verb = $1 eq '+' ? 1 : 0;
619 } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/o) {
620 # Detect and protect standard diffs.
621 # Skip and proceed to next line if premature file end or diff line
622 # numbers implausible (so it might be not a diff at all).
623 if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
625 for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
627 $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/o) {
628 if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
630 for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
632 $$lines[$x] =~ /^--- ./o &&
633 $$lines[$x+1] =~ /^\+\+\+ ./o &&
634 $$lines[$x+2] =~ /^@@ -[0-9]+,([0-9]+) \+[0-9]+,([0-9]+) @@/o
636 # Detect and protect unified diffs.
637 # Proceed only as far as the diff should go.
643 for (my $cminus = 0, my $cplus = 0;
644 $cminus<=$minus && $cplus<=$plus && $x<$#$lines;
647 if ($$lines[$x] !~ /^\+/) { $cminus++; };
648 if ($$lines[$x] !~ /^-/) { $cplus++; };
651 } else { $vrb[$x] = $verb; }
654 # Calculate quoting ratio (with respect to verbatims):
655 if ($check && scalar(@$lines)) {
656 my ($y, $z) = (0, 0);
657 for ($x=0; $x<scalar(@$lines); $x++) {
660 if (index($$lines[$x], $indent)==0) { $y++; }
664 if ($y/$z>=$check_ratio) {
670 if ($mua eq 'mutt') {
671 # See if we find pgp output generated by mutt before we scramble
672 # the thing. If yes, see if we can beautify it.
673 if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
675 # Remove all but the first attachment (if this is text/plain)
676 # mutt did introduce (bah!). Remember, all this ugliness could
677 # be replaced with a proper and clean edit_filter patch in
679 for ($x=$#$lines; $x>=0; $x--) {
680 if ($vrb[$x]) { next; }
681 # The following regexp's are quite ugly because for most users
682 # these lines are coloured using termcap... (bah!)
684 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
689 (?:\Q$mutt_contenttype\E)
690 (?:text/plain|application/pgp)
694 (?:\Q$mutt_pgpsigned\E |
695 \Q$mutt_pgpclearsigned\E|
696 \Q$mutt_pgpcryptend\E|
697 \Q$mutt_pgpencrypted\E)
700 # Strip attachments to prepare further processing
701 unshift(@att, @$lines[$x..$#$lines]);
703 # Try to fix trailing empty lines
704 while (scalar(@$lines) && $$lines[$#$lines] =~
705 /^(?:\e[^\a]+\a)?\s*$/) {
706 unshift(@att, pop(@$lines));
709 # Remove ML and ad footers within attachments:
711 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
712 if ($ad) { remove_footers($lines, \@tmp, $ads, undef); }
713 $x = scalar(@$lines);
717 # care about the rest
719 for ($x=0; $x<$#att; $x++) {
720 if ($vrb[scalar(@$lines)+$x]) { next; }
722 # Pipe message/rfc822 parts to another instance of
723 # process_msg() for further processing.
724 # Please note that we cannot see what a hierarchy the
725 # original message had -- if there were message/rfc822
726 # parts within other message/rfc822 parts constellations
727 # can occur which we cannot resolve. Therefore we simply
728 # do not even try to be smart. This should work for most
730 # The following regexp is quite ugly because for most
731 # users the line is coloured using termcap... (bah!)
733 /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
736 (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
740 while ($att[$x] !~ /^\s*$/) { $x++; }
743 my @tmp = @att[$x..$#att];
745 splice(@att, $x, scalar(@att)-$x, @tmp);
753 if (scalar(@$lines)) {
758 for ($x = $#$lines; $x>=0; $x--) {
760 if ($$lines[$x] =~ /^-- $/) {
762 for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
763 if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
764 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
767 unshift(@sig, @$lines[$x..$#$lines]);
773 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
777 if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
778 if ($lsig && !$sig) {
779 unshift(@sig, "[---=| Overlong signature removed by $0: " .
780 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
785 unshift(@sig, @$lines[$x..$#$lines]);
788 if (defined($sign) && ++$sn==$sign) { last; } else { next; }
790 # any trailing newlines?
791 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
792 elsif ($chk_empty) { $chk_empty = 0; }
797 # Fix quote markers to adhere to RFC 3676, this changes "> >" to ">> " (if
798 # the default $indent is used). The space after ">" is not mandatory by RFC
799 # but makes the result more readable.
801 for ($x=0; $x<scalar(@$lines); $x++) {
802 if (!$vrb[$x] && index($$lines[$x], $indent)==0) {
803 # We match space at the beginning to prevent removal of spaces
804 # directly after the last quote mark.
805 $$lines[$x] =~ /^((\Q$indent\E| )*\Q$indent\E ?)(.*)$/;
808 my $rest = $3; # if anything follows after the quote
810 $tmp .= ' ' if $rest; # don't create trailing whitespace
811 substr($$lines[$x], 0, $len, $tmp);
816 # See if there is some Kammquoting to fix:
817 if ($kamm) { decomb($lines, \@vrb); }
819 # Now care about TOFU.
820 # One common mispractice is M$ and Groupwise style TOFU:
822 # bloat this array if you want more internationalization:
823 my @tofu = ('Original Message',
824 'Original-Nachricht',
825 'Urspr..?ngliche Nachricht',
827 'Ursprungligt meddelande',
828 'Oorspronkelijk bericht',
829 'Message d\'origine',
832 'Weitergeleitete Nachricht / Forwarded Message',
833 '[A-Za-z ]+ <(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})> schrieb:');
834 my $k = 0; # any text above?
835 my $tmp = 0; # flagged if inside PGP or SSL output
837 DONE: for ($x=0; $x<scalar(@$lines); $x++) {
839 foreach my $tmp (@tofu) {
840 if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
842 ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
843 $$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/))) {
846 if ($k) { $bigqn = 0; }
851 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
854 # this line seems not necessary since mutt-1.5.18:
855 $$lines[$x] !~ /^\e[^\a]+\a$/o &&
857 /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
859 /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
860 ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
862 if ($mua eq 'mutt' && (!$tmp) &&
864 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o ||
866 /^(?:\e[^\a]+\a)?(?:\Q$mutt_ssloutstart\E)/o)) {
868 } elsif ($mua eq 'mutt' && $tmp &&
870 /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
872 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpcryptstart\E)/o ||
874 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
883 # try to avoid false positives and only delete m$ style tofu if
884 # there is text above
886 if (!$ms_smart) { goto CLEAN; }
888 # first, see if there is pgp stuff inside the tofu:
889 my $p = 0; # levels of pgp signed parts
891 for (my $i=$x+1; $i<scalar(@$lines); $i++) {
893 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
898 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
899 if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
904 /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
905 splice(@att, 0, $i+1);
913 # now removing is safe:
914 CLEAN: $j = scalar(@$lines)-$x;
919 # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
920 # much more aggressively, so we won't need to search any more if we
922 if ($trad && (!$j) && !$vrb[$#$lines]) {
923 if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) {
924 unshift(@sig, pop(@$lines));
930 for (my $i=$#$lines; $i>=0; $i--) {
931 if ($vrb[$i]) { last; }
932 if (index($$lines[$i], $indent)==0) {
936 elsif ($$lines[$i] !~ /^\s*$/) { last; }
940 # if there is no text above, we will assume the message is meant
941 # as forwarding and therefore OK
942 for (my $i=$k-1; $i>=0; $i--) {
943 if ($$lines[$i] !~ /^\s*$/o) {
956 # OK, if we found TOFU, we will leave a message that we were here...
958 # make sendmail bounce if we shall be picky
959 # and indeed found something:
965 eval { require Sys::Syslog; };
966 if ($@) { warn $@; } else {
967 Sys::Syslog::setlogsock('unix');
968 Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
969 Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
970 Sys::Syslog::closelog();
978 # if we were invoked just for checking and indeed found something,
979 # print out the error message and quit:
985 push(@$lines, "[---=| TOFU protection by $0: " .
986 "$j lines snipped |=---]\n");
988 elsif ($mda eq '1') { exit EX_OK; }
990 # Care for huge blocks of quoted original message:
991 if ($bigqn) { debigq($lines, \@vrb); }
993 # Care for trailing whitespaces:
995 for ($x=0; $x<scalar(@$lines); $x++) {
996 if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
1000 # Care for punctuation abuse:
1002 for ($x=0; $x<scalar(@$lines); $x++) {
1003 if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
1007 # (Nearly) at last care for multiple blank lines. (Do not do this
1008 # earlier -- the way it is done right now would screw up the verbatim
1011 # When handling regular TOFU above we move the last empty line from
1012 # the body to the signature. This prevents the correct removal of
1013 # empty lines before a signature (one line less is removed than it
1014 # should) when -c and -t are used. This fixes it.
1015 if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
1016 push(@$lines, shift(@sig));
1020 for ($x=scalar(@$lines)-1; $x>=0; $x--) {
1022 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
1023 $$lines[$x] =~ /^\s*$/o))
1025 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
1031 # Everything changing the body is done now. Time to fix the line count
1032 # header so naive clients do not get confused. Just to be sure, append
1033 # the old line count to X-headers.
1034 my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
1035 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) +
1036 (!$ad?scalar(@ads):0);
1037 if ($linecount-$l!=0) {
1038 for ($x=0; $x<scalar(@hdr); $x++) {
1040 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1042 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1048 # Finally, before leaving we put everything back in right order.
1049 unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1050 push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1056 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1057 ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1059 # command line switches
1060 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1061 $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1062 $sani, $sig, $sigint, $spass, $trad, $trsp) =
1063 (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);
1064 my $ifile = '-'; # use STDIN if nothing specified
1067 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1068 $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1070 # get command line params:
1071 if (!Getopt::Long::GetOptions(
1074 'bigq:s' => \$_t_bigq,
1075 'body' => \$_t_nohdr,
1077 'check:s' => \$_t_check,
1078 'debug|d' => \$sysl,
1081 'fixind' => \$fixind,
1082 'ftr-ad' => \$ftr_ad,
1083 'ftr-ml' => \$ftr_ml,
1084 'groupwise' => \$gw,
1085 'help|h' => \$_t_help,
1088 'kminl=i' => \$_t_kminl,
1089 'kmaxl=i' => \$_t_kmaxl,
1090 'kdiff=i' => \$_t_kdiff,
1093 'lax-security' => \$lax,
1094 'locale=s' => \$locale,
1095 'max-lines:i' => \$maxlines,
1096 'ms-smart' => \$ms_smart,
1097 'mua|M=s' => \$_t_mua,
1101 'p:s' => \$_t_redir,
1102 'pgp-short' => \$pgpshort,
1103 'pgp-move' => \$pgpmove,
1104 'pgp-move-vrf' => \$pgpmovevrf,
1107 'S:i' => \$_t_maxsig,
1110 'sigsmax:i' => \$_t_sigsmax,
1113 'version|v' => \$_t_ver,
1119 # clean up temp vals:
1120 if (defined $_t_bigq) {
1121 if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1124 if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1126 if (defined $_t_help) { help(); }
1127 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1128 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1129 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1130 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1131 if (defined $_t_mua) {
1133 # mutt still displays the message when ^C'ing pgp verification:
1134 if ($mua eq 'mutt' || $mua eq 'mutt-kz') {
1135 $SIG{'INT'} = 'sigint_handler';
1138 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1139 if (defined $_t_check) {
1141 while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1144 if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1147 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1148 if (defined $_t_ver) { version(); }
1149 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1150 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1151 if ($ms_smart) { $ms = 1; }
1153 if ($mua eq 'mutt' || $mua eq 'mutt-kz') {
1154 if (defined $locale &&
1155 $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1156 eval { require Locale::gettext; };
1157 if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1158 Locale::gettext::textdomain($mua);
1159 ($mutt_attachment) =
1160 Locale::gettext::gettext("[-- Attachment #%d") =~
1162 ($mutt_contenttype) =
1163 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1166 Locale::gettext::gettext("\n[-- End of signed data --]\n") =~
1168 ($mutt_beginsigned) =
1169 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1171 ($mutt_pgpclearsigned) =
1172 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1174 ($mutt_pgpclearsigstart) =
1175 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n\n") =~
1177 ($mutt_pgpencrypted) =
1178 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1180 ($mutt_pgpoutstart) =
1181 sprintf(Locale::gettext::gettext("[-- %s output follows%s --]\n"), 'PGP', '%s') =~
1183 $mutt_pgpoutstart =~ s/%s/PGP/;
1185 Locale::gettext::gettext("[-- End of PGP output --]\n\n") =~
1187 ($mutt_ssloutstart) =
1188 Locale::gettext::gettext("[-- %s output follows%s --]\n") =~
1190 $mutt_ssloutstart =~ s/%s/OpenSSL/;
1192 Locale::gettext::gettext("[-- End of OpenSSL output --]\n\n") =~
1195 Locale::gettext::textdomain('gnupg');
1197 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1200 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1203 Locale::gettext::gettext("Can't check signature: %s\n") =~
1206 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1209 Locale::gettext::gettext('BAD signature from "%s"') =~
1212 Locale::gettext::gettext("Note: This key has expired!\n") =~
1215 Locale::gettext::gettext('Good signature from "%s"') =~
1216 m/^(([^"«%]*))["«%]/;
1218 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1221 # mutt and mutt-kz right now only differ in the gettext domain name,
1222 # so after fetching the gettext lines we treat them the same:
1227 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1229 if (($ml && $footers eq '')|| # no -l without -L
1230 ($ad && $ads eq '')|| # no -a without -A
1231 ($nohdr && $pgpshort)|| # --body and --pgp-short are like oil and water
1232 (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1233 ($ifile eq '')|| # no empty -i
1234 ($ofile eq '')) # no empty -o
1239 if (!open(IN, $ifile))
1240 { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1244 # First, check msg length and stop processing if msg is too long:
1245 if ((defined $maxlines) && (@message > $maxlines)) {
1246 if ($mua eq 'mutt') {
1249 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1252 splice(@message, $x, 0,
1253 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1255 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1257 if ($mua eq 'mutt') { exit(EX_OK); }
1261 # this should be self-explanatory:
1262 process_msg(\@message);
1264 # Finally, print clean lines:
1265 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1267 # vim600:set foldmethod=marker: