]> git.deb.at Git - pkg/t-prot.git/blob - t-prot
Imported Upstream version 3.4
[pkg/t-prot.git] / t-prot
1 #!/usr/bin/perl -w
2 # $Id: t-prot,v 1.330 2015/03/26 08:59:50 jochen Exp $
3
4 require 5.006;
5 use strict;
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';
14 # From <sysexits.h>
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;
23 use vars qw(
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
29
30     $gpg_WARNING $gpg_Warning $gpg_Cantcheck $gpg_aka $gpg_bad
31     $gpg_expired $gpg_good $gpg_bug
32
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
37 );
38
39
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.
43 $0 =~ s!^.*/!!;
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
49 $kmaxl          = 80;
50 $kdiff          = 20;
51 $pgpshort       = 0;      # hide pgp key ids if set
52 $pgpmove        = 0;      # move pgp output to bottom if set
53 $pgpmovevrf     = 0;
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
64
65
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 --]';
80
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: ';
86 $gpg_aka                = '                aka ';
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 (';
92
93
94 # help(): print help text and exit with appropriate exit code
95 sub help {
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
120                   footers as signature
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)
137   -s              delete signature
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";
144     exit(EX_USAGE);
145 }
146
147 # version(): print version info and exit with appropriate exit code
148 sub version {
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";
151     exit(EX_OK);
152 }
153
154 # sigint_handler(): what to do if we receive a single SIGINT
155 sub sigint_handler {
156     $sigint = 1;
157 }
158
159 sub sanitize_hdr {
160     # Undoes MIME quoted-printable word encoding.
161     sub qp_decode {
162         my $word = shift;
163         $word =~ tr/_/\x20/;
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;
172         $word;
173     }
174
175     sub umlauts {
176         my $word = shift;
177         $word =~ s/ä/ae/og;
178         $word =~ s/Ä/Ae/og;
179         $word =~ s/ö/oe/og;
180         $word =~ s/Ö/Oe/og;
181         $word =~ s/ü/ue/og;
182         $word =~ s/Ü/Ue/og;
183         $word =~ s/ß/ss/og;
184         $word;
185     }
186
187     my $H = shift;        # array of header lines
188     my $i;
189
190     for ($i=0; $i<$#$H; $i++) {
191         if ($$H[$i] =~ /^subject: /io) {
192             if ($sani) {
193                 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
194                 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
195             }
196             if ($reply) {
197                 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
198             }
199
200             $i++;
201             while ($i<$#$H && $$H[$i] =~ /^\s/) {
202                 if ($sani) {
203                     $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
204                     $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
205                 }
206                 if ($reply) {
207                     $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
208                 }
209                 $i++;
210             }
211         }
212         elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
213             $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
214
215             $i++;
216             while ($i<$#$H && $$H[$i] =~ /^\s/) {
217                 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
218                 $i++;
219             }
220         }
221     }
222 }
223
224 # remove_footers(): remove any trailing appearance of footers contained
225 # in the given directory.
226 sub remove_footers {
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
232     my $off;
233
234     if (!defined $V) {
235         for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
236     }
237
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;
242         closedir DIR;
243
244         foreach my $f (@feet) {
245             if (!open(IN, "$F/$f"))
246                 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
247             my @l = <IN>;
248             close IN;
249
250             if (!scalar(@l)) { next; }
251             for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
252
253             if (defined $V) {
254                 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
255                     if (scalar(@l)+$z<=scalar(@$L)) {
256                         my $y = 0;
257                         for(my $x=1; $x<=scalar(@l); $x++) {
258                             if (index($$L[scalar(@$L)-$x-$z],
259                                           $l[scalar(@l)-$x])!=0) {
260                                 $y = 1;
261                             }
262                         }
263                         if (!$y) {
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));
268                             }
269                             if ($O) { last; } else { goto WIPE; }
270                         }
271                     }
272                 }
273             }
274             else {
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) { 
278                             goto FINISH;
279                         }
280                     }
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));
285                     }
286                     if ($O) { last; }
287                 }
288                 FINISH:
289             }
290         }
291     }
292 }
293
294 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
295 # "Kammquoting").
296 sub decomb {
297     my $L = shift;        # array of message lines
298     my $V = shift;        # array with verbatim list
299     my $max = 0;        # plausible wraparound pos
300
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]); }
305     }
306
307     # Next we see if there are plausible wraparounds.    
308     for (my $x=0; $x+1<$#$L; $x++) {
309
310         # OK, 
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^]/) &&
331             ($$L[$x]   !~ /-$/) &&
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*$/))
341         {
342             $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
343             splice(@$L, $x+1, 1);
344             splice(@$V, $x+1, 1);
345         }
346     }
347 }
348
349 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
350 # but the last $x lines of them.
351 sub debigq {
352     my $L = shift;        # array of message lines
353     my $V = shift;        # array with verbatim list
354     my $k = 0;
355
356     for (my $i=0; $i<=$#$L; $i++) {
357
358         if ($$V[$i]) { 
359             $k = 0;
360             next;
361         }
362
363         if (index($$L[$i], $indent)==0) { $k++; } else { 
364             if ($k>$bigqn) {
365                 my $x = $k-$bigqx;
366                 $i -= $k;
367
368                 $$L[$i] = "[---=| Quote block shrunk by $0: " .
369                     "$x lines snipped |=---]\n";
370                 $i++;
371                 splice(@$L, $i, $x-1);
372                 splice(@$V, $i, $x-1);
373
374                 $i++;
375             }
376             $k = 0;
377         }
378     }
379 }
380
381 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
382 # messages
383 sub pgp {
384
385     sub verified {
386         my $L = shift;    # message body
387         my $X = shift;    # start line
388         my $Z = shift;    # end line
389
390                 my $ok = 0;
391
392         while ($X<$Z) {
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)
399                 { return 0; }
400             if (index($$L[$X], "gpg: $gpg_good")==0)
401                 { $ok = 1; }
402             $X++;
403         }
404
405         return $ok;
406     }
407
408     my $L = shift;        # message body
409     my $V = shift;        # verbatim list
410     my $H = shift;        # headers
411
412     my @tmp = ();
413     my $tmp = 0;
414
415     for (my $x=0; $x<scalar(@$L); $x++) {
416         if ($$V[$x]) { next; }
417
418         if ($$L[$x]=~/^(?:\e[^\a]+\a)?
419                        (?:\Q$mutt_pgpoutstart\E|\Q$mutt_ssloutstart\E)/ox) {
420             my $from;
421             if ($pgpshort) {
422                 for (my $m=0; $m<scalar(@$H); $m++) {
423                     if (index($$H[$m], 'From:')==0) {
424                         $from = $$H[$m];
425                         $m++;
426                         while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
427                             { $from .= $$H[$m]; $m++; }
428                         last;
429                     }
430                 }
431                 if (defined($from)) {
432                     ($from) =
433                         $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
434                 }
435             }
436
437             my $uid = 1;
438
439             for (my $i=$x+1; $i<scalar(@$L); $i++) {
440                 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
441                     $uid++;
442
443                     if (!defined($from) ||
444                        ($uid>1 && index($$L[$i], $from)<0)) {
445                         splice(@$L, $i, 1);
446                         splice(@$V, $i, 1);
447                         $i--;
448                     }
449                 }
450                 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?
451                        (?:\Q$mutt_pgpoutend\E|\Q$mutt_ssloutend\E)/ox) {
452                     if ($pgpmove ||
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;
458                     }
459                     $x = $i;
460                     last;
461                 }
462             }
463         }
464         elsif ($tmp &&
465                $$L[$x] =~ /^
466                         (?:\e[^\a]+\a)?
467                         (?:\Q$mutt_pgpencrypted\E|
468                            \Q$mutt_pgpcryptend\E|
469                            \Q$mutt_pgpclearsigned\E|
470                            \Q$mutt_pgpsigned\E)
471                        /ox) {
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));
475             }
476             $x += scalar(@{$tmp[$tmp--]});
477             pop(@tmp);
478         }
479     }
480
481     while ($tmp>0) {
482         push(@$L, @{$tmp[$tmp--]});
483         pop(@tmp);
484         for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
485     }
486 }
487
488 # write_msg(): output
489 sub write_msg {
490     my $O = shift;
491     my $l;
492
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);
496         }
497     }
498     elsif (!open(OUT, $O)) { 
499         print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
500     }
501     while (scalar(@_)) {
502         $l = shift;
503         if (defined $l) {
504             $^W = 0;
505             print OUT @$l;
506             $^W = 1;
507         }
508     }
509     close OUT;
510 }
511
512 # process_msg(): This one proc does *everything* what has to be done with
513 # the lines of the message
514 sub process_msg {
515     my $lines = shift;
516
517     my ($j, $x, $verb) = (0, 0, 0);
518     my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) = 
519         ((), (), (), (), (), (), (), (), ());
520
521     # Remove and store lines we might need later...
522     # Remove headers:
523     if (!$nohdr) {
524         for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
525         @hdr = @$lines[0..$x];
526         splice(@$lines, 0, $x+1);
527     }
528     # remember the original body lines count
529     my $linecount = scalar(@$lines);
530
531     # Sanitize header fields:
532     if ($reply || $sani) { sanitize_hdr(\@hdr); }
533
534     # Remove ML footers:
535     remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
536
537     # Remove ad footers:
538     remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
539
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+(.*)$/) {
547                 my $foo = $1;
548
549                 if ($foo =~ m!^multipart/!) {
550                     undef $foo;
551
552                     if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
553                     else { 
554                         for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
555                             if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) { 
556                                 $foo = $1;
557                                 last;
558                             }
559                         }
560                     }
561
562                     if (defined $foo) {
563                         for (my $x=0; $x<scalar(@$lines); $x++) {
564                             if (index($$lines[$x], '--'.$foo)!=0) { next; }
565
566                             my $bar = 'text/plain';
567                             for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
568                             {
569                                 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) { 
570                                     $bar = $1;
571                                 }
572                             }
573                             if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
574
575                             if ($bar =~ m!^text/plain!i) {
576                                 my $z;
577                                 for ($z=1; $x+$z<@$lines; $z++) {
578                                     if (index($$lines[$x+$z], '--'.$foo)==0) {
579                                         last;
580                                     }
581                                 }
582                                 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
583
584                                 @bo2 = @$lines[$x+$z..$#$lines];
585                                 splice(@$lines, $x+$z);
586                                 if ($$lines[$#$lines] =~ /^\s*$/) {
587                                     unshift(@bo2, pop @$lines);
588                                 }
589                                 @bo1 = @$lines[0..$x];
590                                 splice(@$lines, 0, $x+1);
591
592                                 # remove mailing list and ad footers within this
593                                 # attachment:
594                                 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
595                                 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
596
597                                 last;
598                             }
599                             else { 
600                                 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
601                                     ($hdrs?undef:\@hdr), $lines);
602                                 exit;
603                             }
604                         }
605                     }
606                 }
607                 last;
608             }
609         } 
610     }
611
612
613     # Protect verbatims:
614     $verb = 0;
615     for ($x=0; $x<scalar(@$lines); $x++) {
616         if ($$lines[$x] =~ /^\s*#v([+-])$/o) { 
617             $verb = $1 eq '+' ? 1 : 0;
618             $vrb[$x] = 1;
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; }
624             $vrb[$x] = 1;
625             for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
626         } elsif ($diff &&
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; }
629             $vrb[$x] = 1;
630             for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
631         } elsif ($diff &&
632                  $$lines[$x] =~ /^--- ./o &&
633                  $$lines[$x+1] =~ /^\+\+\+ ./o &&
634                  $$lines[$x+2] =~ /^@@ -[0-9]+,([0-9]+) \+[0-9]+,([0-9]+) @@/o
635                 ) {
636             # Detect and protect unified diffs.
637             # Proceed only as far as the diff should go.
638             my $minus = $1;
639             my $plus = $2;
640             $vrb[$x++] = 1;
641             $vrb[$x++] = 1;
642             $vrb[$x++] = 1;
643             for (my $cminus = 0, my $cplus = 0;
644                  $cminus<=$minus && $cplus<=$plus && $x<$#$lines;
645                  $x++) {
646                 $vrb[$x] = 1;
647                 if ($$lines[$x] !~ /^\+/) { $cminus++; };
648                 if ($$lines[$x] !~ /^-/) { $cplus++; };
649             }
650             $x-=1;
651         } else { $vrb[$x] = $verb; }
652     }
653
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++) {
658             if (!$vrb[$x]) {
659                 $z++;
660                 if (index($$lines[$x], $indent)==0) { $y++; }
661             }
662         }
663
664         if ($y/$z>=$check_ratio) {
665             print $msg_ratio;
666             exit EX_UNAVAILABLE;
667         }
668     }
669
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); }
674
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 
678         # mutt(1) itself...
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!)
683             if (($$lines[$x] =~
684                      /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
685                     (($1 ne '1') ||
686                     ($x<$#$lines &&
687                         $$lines[$x+1] !~ m!^
688                             (?:\e[^\a]+\a)?
689                             (?:\Q$mutt_contenttype\E)
690                             (?:text/plain|application/pgp)
691                         !oxi))) ||
692                 ($$lines[$x] =~ /^
693                     (?:\e[^\a]+\a)?
694                     (?:\Q$mutt_pgpsigned\E     |
695                        \Q$mutt_pgpclearsigned\E|
696                        \Q$mutt_pgpcryptend\E|
697                        \Q$mutt_pgpencrypted\E)
698                 /ox))
699             { 
700                 # Strip attachments to prepare further processing
701                 unshift(@att, @$lines[$x..$#$lines]);
702                 splice(@$lines, $x);
703                 # Try to fix trailing empty lines
704                 while (scalar(@$lines) && $$lines[$#$lines] =~
705                     /^(?:\e[^\a]+\a)?\s*$/) { 
706                     unshift(@att, pop(@$lines));
707                 }
708
709                 # Remove ML and ad footers within attachments:
710                 my @tmp;
711                 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
712                 if ($ad) { remove_footers($lines, \@tmp, $ads,     undef); }
713                 $x = scalar(@$lines);
714             }
715         }
716
717         # care about the rest
718         if (scalar(@att)) {
719             for ($x=0; $x<$#att; $x++) {
720                 if ($vrb[scalar(@$lines)+$x]) { next; }
721
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
729                 # situations.
730                 # The following regexp is quite ugly because for most
731                 # users the line is coloured using termcap... (bah!)
732                 if ($att[$x] =~
733                         /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
734                     $att[$x+1] =~ m!^
735                         (?:\e[^\a]+\a)?
736                         (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
737                     !ox)
738                 {
739                     $x += 2;
740                     while ($att[$x] !~ /^\s*$/) { $x++; }
741                     $x++;
742
743                     my @tmp = @att[$x..$#att];
744                     process_msg(\@tmp);
745                     splice(@att, $x, scalar(@att)-$x, @tmp);
746                     $x += scalar(@tmp);
747                 }
748             }
749         }
750     }
751
752     # Remove signature:
753     if (scalar(@$lines)) { 
754         my $sn = 0;
755         my $chk_empty = 1;
756         my $empty = 0;
757
758         for ($x = $#$lines; $x>=0; $x--) {
759             if (!$vrb[$x]) {
760                 if ($$lines[$x] =~ /^-- $/) {
761                     if ($diff) {
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/)
765                             {
766                                 $sig = 0;
767                                 unshift(@sig, @$lines[$x..$#$lines]);
768                                 splice(@$lines, $x);
769                                 last;
770                             }
771                         }
772                         if (scalar(@sig)) {
773                             if (defined($sign) && ++$sn==$sign) { last; } else { next; }
774                         }
775                     }
776
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");
781                         }
782                         splice(@$lines, $x);
783                     }
784                     else {
785                         unshift(@sig, @$lines[$x..$#$lines]);
786                         splice(@$lines, $x);
787                     }
788                     if (defined($sign) && ++$sn==$sign) { last; } else { next; }
789                 }
790                 # any trailing newlines?
791                 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
792                 elsif ($chk_empty) { $chk_empty = 0; }
793             }
794         }
795     }
796
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.
800     if ($fixind) {
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 ?)(.*)$/;
806                 my $tmp = $1;
807                 my $len = length $1;
808                 my $rest = $3;           # if anything follows after the quote
809                 $tmp =~ tr/ //d;
810                 $tmp .= ' ' if $rest;    # don't create trailing whitespace
811                 substr($$lines[$x], 0, $len, $tmp);
812             }
813         }
814     }
815
816     # See if there is some Kammquoting to fix:
817     if ($kamm) { decomb($lines, \@vrb); }
818
819     # Now care about TOFU.
820     # One common mispractice is M$ and Groupwise style TOFU:
821     if ($ms||$gw) {
822         # bloat this array if you want more internationalization:
823         my @tofu = ('Original Message',
824                     'Original-Nachricht',
825                     'Urspr..?ngliche Nachricht',
826                     'Mensagem original',
827                     'Ursprungligt meddelande',
828                     'Oorspronkelijk bericht',
829                     'Message d\'origine',
830                     'Origin message',
831                     'Forwarded message',
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
836
837         DONE: for ($x=0; $x<scalar(@$lines); $x++) { 
838             if (!$vrb[$x]) {
839                 foreach my $tmp (@tofu) {
840                     if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
841                         ($gw &&
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/))) { 
844                         $x++;
845                         $trad = 0;
846                         if ($k) { $bigqn = 0; }
847                         last DONE; 
848                     }
849                 }
850
851                 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
852                     ((!$mua) ||
853                      ($mua eq 'mutt' &&
854                          # this line seems not necessary since mutt-1.5.18:
855                          $$lines[$x] !~ /^\e[^\a]+\a$/o &&
856                          $$lines[$x] !~
857                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
858                          $$lines[$x] !~
859                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
860                     ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
861                 {
862                     if ($mua eq 'mutt' && (!$tmp) &&
863                         ($$lines[$x] =~
864                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o ||
865                          $$lines[$x] =~
866                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_ssloutstart\E)/o)) {
867                         $tmp = 1;
868                     } elsif ($mua eq 'mutt' && $tmp && 
869                         ($$lines[$x] =~
870                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
871                          $$lines[$x] =~
872                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpcryptstart\E)/o ||
873                          $$lines[$x] =~
874                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
875                         $tmp = 0;
876                     } elsif (!$tmp) {
877                         $k = 1;
878                     }
879                 }
880             }
881         }
882
883         # try to avoid false positives and only delete m$ style tofu if
884         # there is text above
885         if ($k) {
886             if (!$ms_smart) { goto CLEAN; }
887
888             # first, see if there is pgp stuff inside the tofu:
889             my $p = 0;    # levels of pgp signed parts
890
891             for (my $i=$x+1; $i<scalar(@$lines); $i++) {
892                 if ($$lines[$i] =~
893                         /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
894                     $p++;
895                 }
896             }
897             if ($p) {
898                 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
899                     if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
900                         splice(@att, 0, $i);
901                         unshift(@att, "\n");
902                         goto CLEAN;
903                     } elsif ($att[$i] =~
904                             /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
905                         splice(@att, 0, $i+1);
906                         $p--;
907                         goto STAIRS;
908                     }
909                 }
910                 splice(@att);
911             }
912
913             # now removing is safe:
914             CLEAN: $j = scalar(@$lines)-$x;
915             splice(@$lines, $x);
916         }
917     }
918
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
921     # did find some):
922     if ($trad && (!$j) && !$vrb[$#$lines]) {
923         if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) { 
924             unshift(@sig, pop(@$lines));
925         }
926
927         my $k;
928         my $x = 1;
929
930         for (my $i=$#$lines; $i>=0; $i--) {
931             if ($vrb[$i]) { last; }
932             if (index($$lines[$i], $indent)==0) {
933                 $j++;
934                 $k = $i;
935             }
936             elsif ($$lines[$i] !~ /^\s*$/) { last; }
937         }
938
939         if ($j) {
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) {
944                    $x = 0;
945                    last;
946                 }
947             }
948             if ($x) {
949                 $j = 0;
950             } else {
951                 splice(@$lines, $k);
952             }
953         }
954     }
955
956     # OK, if we found TOFU, we will leave a message that we were here...
957     if ($j) { 
958         # make sendmail bounce if we shall be picky 
959         # and indeed found something:
960         if ($mda) { 
961             if ($mda ne '1') { 
962                 print STDERR $boun;
963
964                 if ($sysl) {
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();
971                     }
972                 }
973             }
974
975             exit EX_BOUNCE;
976         }
977
978         # if we were invoked just for checking and indeed found something,
979         # print out the error message and quit:
980         if ($check) {
981             print $msg_quote;
982             exit EX_UNAVAILABLE;
983         }
984
985         push(@$lines, "[---=| TOFU protection by $0: " .
986             "$j lines snipped |=---]\n");
987     }
988     elsif ($mda eq '1') { exit EX_OK; }
989
990     # Care for huge blocks of quoted original message:
991     if ($bigqn) { debigq($lines, \@vrb); }
992
993     # Care for trailing whitespaces:
994     if ($trsp) {
995         for ($x=0; $x<scalar(@$lines); $x++) { 
996             if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
997         }
998     }
999
1000     # Care for punctuation abuse:
1001     if ($elli) {
1002         for ($x=0; $x<scalar(@$lines); $x++) { 
1003             if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
1004         }
1005     }
1006
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
1009     # list)
1010     if ($cr) {
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));
1017         }
1018
1019         my $t = 0;
1020         for ($x=scalar(@$lines)-1; $x>=0; $x--) {
1021             if ((!$vrb[$x]) &&
1022                 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
1023                   $$lines[$x] =~ /^\s*$/o))
1024             { 
1025                 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
1026             }
1027             else { $t = 0; }
1028         }
1029     }
1030
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++) {
1039             if ($hdr[$x] =~ 
1040                 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1041             { 
1042                 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1043                 push(@hdr, "\n");
1044             }
1045         }
1046     }
1047
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,
1051         @bo2);
1052 }
1053
1054
1055 # environment
1056 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1057                  ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1058
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
1065
1066 # temp vals:
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;
1069
1070 # get command line params:
1071 if (!Getopt::Long::GetOptions(
1072     'a'             => \$ad,
1073     'A=s'           => \$ads,
1074     'bigq:s'        => \$_t_bigq,
1075     'body'          => \$_t_nohdr,
1076     'c:i'           => \$_t_cr,
1077     'check:s'       => \$_t_check,
1078     'debug|d'       => \$sysl,
1079     'diff'          => \$diff,
1080     'e'             => \$elli,
1081     'fixind'        => \$fixind,
1082     'ftr-ad'        => \$ftr_ad,
1083     'ftr-ml'        => \$ftr_ml,
1084     'groupwise'     => \$gw,
1085     'help|h'        => \$_t_help,
1086     'i=s'           => \$ifile,
1087     'k'             => \$kamm,
1088     'kminl=i'       => \$_t_kminl,
1089     'kmaxl=i'       => \$_t_kmaxl,
1090     'kdiff=i'       => \$_t_kdiff,
1091     'L=s'           => \$footers,
1092     'l'             => \$ml,
1093     'lax-security'  => \$lax,
1094     'locale=s'      => \$locale,
1095     'max-lines:i'   => \$maxlines,
1096     'ms-smart'      => \$ms_smart,
1097     'mua|M=s'       => \$_t_mua,
1098     'm'             => \$ms,
1099     'o=s'           => \$ofile,
1100     'P=s'           => \$boun,
1101     'p:s'           => \$_t_redir,
1102     'pgp-short'     => \$pgpshort,
1103     'pgp-move'      => \$pgpmove,
1104     'pgp-move-vrf'  => \$pgpmovevrf,
1105     'r'             => \$hdrs,
1106     'reply'         => \$reply,
1107     'S:i'           => \$_t_maxsig,
1108     's'             => \$sig,
1109     'sani'          => \$sani,
1110     'sigsmax:i'     => \$_t_sigsmax,
1111     'spass'         => \$spass,
1112     't'             => \$trad,
1113     'version|v'     => \$_t_ver,
1114     'w'             => \$trsp
1115     )) {
1116     help();
1117 }
1118
1119 # clean up temp vals:
1120 if (defined $_t_bigq) {
1121     if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1122     $bigqn = $1?$1:30; 
1123     $bigqx = $2?$2:10; 
1124     if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1125 }
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) {
1132     $mua = lc($_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';
1136     }
1137 }
1138 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1139 if (defined $_t_check) {
1140     $check = 1;
1141     while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1142         my $foo = $1;
1143         $_t_check = $2;
1144         if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1145     }
1146 }
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; }
1152
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") =~
1161                 m/^([^%]*)/;
1162             ($mutt_contenttype) =
1163                 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1164                 m/^([^%]*)/;
1165             ($mutt_pgpsigned) =
1166                 Locale::gettext::gettext("\n[-- End of signed data --]\n")  =~
1167                 m/^\n*(.*)\n/m;
1168             ($mutt_beginsigned) =
1169                 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1170                 m/^(.*)\n/m;
1171             ($mutt_pgpclearsigned) =
1172                 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1173                 m/^(.*)\n/m;
1174             ($mutt_pgpclearsigstart) =
1175                 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n\n") =~
1176                 m/^(.*)\n/m;
1177             ($mutt_pgpencrypted) =
1178                 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1179                 m/^(.*)\n/m;
1180             ($mutt_pgpoutstart) =
1181                 sprintf(Locale::gettext::gettext("[-- %s output follows%s --]\n"), 'PGP', '%s') =~
1182                 m/^(.*)%s/;
1183             $mutt_pgpoutstart =~ s/%s/PGP/;
1184             ($mutt_pgpoutend) =
1185                 Locale::gettext::gettext("[-- End of PGP output --]\n\n") =~
1186                 m/^(.*)\n/m;
1187             ($mutt_ssloutstart) =
1188                 Locale::gettext::gettext("[-- %s output follows%s --]\n") =~
1189                 m/^(.*)%s/;
1190             $mutt_ssloutstart =~ s/%s/OpenSSL/;
1191             ($mutt_ssloutend) =
1192                 Locale::gettext::gettext("[-- End of OpenSSL output --]\n\n") =~
1193                 m/^(.*)\n/m;
1194
1195             Locale::gettext::textdomain('gnupg');
1196             ($gpg_WARNING) =
1197                 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1198                 m/^([^:]*: )/;
1199             ($gpg_Warning) =
1200                 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1201                 m/^([^:]*: )/;
1202             ($gpg_Cantcheck) =
1203                 Locale::gettext::gettext("Can't check signature: %s\n") =~
1204                 m/^([^%]*)/;
1205             ($gpg_aka) =
1206                 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1207                 m/^([^"«%]*)["«%]/;
1208             ($gpg_bad) =
1209                 Locale::gettext::gettext('BAD signature from "%s"') =~
1210                 m/^([^"«%]*)["«%]/;
1211             ($gpg_expired) =
1212                 Locale::gettext::gettext("Note: This key has expired!\n") =~
1213                 m/^(.*)\n/m;
1214             ($gpg_good) =
1215                 Locale::gettext::gettext('Good signature from "%s"') =~
1216                 m/^(([^"«%]*))["«%]/;
1217             ($gpg_bug) =
1218                 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1219                 m/^([^%]*)/;
1220         }
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:
1223         $mua = 'mutt';
1224     }
1225
1226 }
1227 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1228  
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
1235 { help(); }
1236
1237
1238 # Read message:
1239 if (!open(IN, $ifile))
1240     { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1241 my @message = <IN>;
1242 close IN;
1243
1244 # First, check msg length and stop processing if msg is too long:
1245 if ((defined $maxlines) && (@message > $maxlines)) {
1246     if ($mua eq 'mutt') {
1247         my $x = 0;
1248         if (!$nohdr) {
1249             do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1250             $x++;
1251         }
1252         splice(@message, $x, 0,
1253             ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1254     }
1255     write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1256
1257     if ($mua eq 'mutt') { exit(EX_OK); }
1258     exit(EX_DATAERR);
1259 }
1260
1261 # this should be self-explanatory:
1262 process_msg(\@message);
1263
1264 # Finally, print clean lines:
1265 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1266
1267 # vim600:set foldmethod=marker:
1268 # eof