]> git.deb.at Git - pkg/t-prot.git/blob - t-prot
Imported Upstream version 3.0
[pkg/t-prot.git] / t-prot
1 #!/usr/bin/perl -w
2 # $Id: t-prot,v 1.319 2013/10/29 11:39:43 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.0';
9 use constant REV            => '';
10 use constant REL            => q$Revision: 1.319 $=~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             for (my $m=0; $m<scalar(@$H); $m++) {
422                 if (index($$H[$m], 'From:')==0) {
423                     $from = $$H[$m];
424                     $m++;
425                     while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
426                         { $from .= $$H[$m]; $m++; }
427                     last;
428                 }
429             }
430             ($from) = $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
431
432             my $uid = 1;
433
434             for (my $i=$x+1; $i<scalar(@$L); $i++) {
435                 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
436                     $uid++;
437
438                     if ($uid>1 && index($$L[$i], $from)<0) { 
439                         splice(@$L, $i, 1);
440                         splice(@$V, $i, 1);
441                         $i--;
442                     }
443                 }
444                 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?
445                        (?:\Q$mutt_pgpoutend\E|\Q$mutt_ssloutend\E)/ox) {
446                     if ($pgpmove ||
447                         ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i))) {
448                         push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
449                         splice(@$L, $x, $i-$x+2);
450                         splice(@$V, $x, $i-$x+2);
451                         $i -= $#{$tmp[$tmp]}-2;
452                     }
453                     $x = $i;
454                     last;
455                 }
456             }
457         }
458         elsif ($tmp &&
459                $$L[$x] =~ /^
460                         (?:\e[^\a]+\a)?
461                         (?:\Q$mutt_pgpencrypted\E|
462                            \Q$mutt_pgpcryptend\E|
463                            \Q$mutt_pgpclearsigned\E|
464                            \Q$mutt_pgpsigned\E)
465                        /ox) {
466             splice(@$L, $x+1, 0, @{$tmp[$tmp]});
467             for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
468                 splice(@$V, $x+1, 0, (0));
469             }
470             $x += scalar(@{$tmp[$tmp--]});
471             pop(@tmp);
472         }
473     }
474
475     while ($tmp>0) {
476         push(@$L, @{$tmp[$tmp--]});
477         pop(@tmp);
478         for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
479     }
480 }
481
482 # write_msg(): output
483 sub write_msg {
484     my $O = shift;
485     my $l;
486
487     if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
488         if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) { 
489             print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
490         }
491     }
492     elsif (!open(OUT, $O)) { 
493         print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
494     }
495     while (scalar(@_)) {
496         $l = shift;
497         if (defined $l) {
498             $^W = 0;
499             print OUT @$l;
500             $^W = 1;
501         }
502     }
503     close OUT;
504 }
505
506 # process_msg(): This one proc does *everything* what has to be done with
507 # the lines of the message
508 sub process_msg {
509     my $lines = shift;
510
511     my ($j, $x, $verb) = (0, 0, 0);
512     my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) = 
513         ((), (), (), (), (), (), (), (), ());
514
515     # Remove and store lines we might need later...
516     # Remove headers:
517     if (!$nohdr) {
518         for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
519         @hdr = @$lines[0..$x];
520         splice(@$lines, 0, $x+1);
521     }
522     # remember the original body lines count
523     my $linecount = scalar(@$lines);
524
525     # Sanitize header fields:
526     if ($reply || $sani) { sanitize_hdr(\@hdr); }
527
528     # Remove ML footers:
529     remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
530
531     # Remove ad footers:
532     remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
533
534     # See if we have a multipart content type. If yes, see if it is already
535     # ripped (e.g. by you MUA, assuming it does not get special treatment with
536     # --mua=foo), otherwise only leave the first part if it is plain text (if
537     # not, we are done - non-text messages are not our business).
538     if ($mua ne 'mutt') { 
539         for ($x=0; $x<scalar(@hdr); $x++) {
540             if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
541                 my $foo = $1;
542
543                 if ($foo =~ m!^multipart/!) {
544                     undef $foo;
545
546                     if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
547                     else { 
548                         for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
549                             if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) { 
550                                 $foo = $1;
551                                 last;
552                             }
553                         }
554                     }
555
556                     if (defined $foo) {
557                         for (my $x=0; $x<scalar(@$lines); $x++) {
558                             if (index($$lines[$x], '--'.$foo)!=0) { next; }
559
560                             my $bar = 'text/plain';
561                             for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
562                             {
563                                 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) { 
564                                     $bar = $1;
565                                 }
566                             }
567                             if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
568
569                             if ($bar =~ m!^text/plain!i) {
570                                 my $z;
571                                 for ($z=1; $x+$z<@$lines; $z++) {
572                                     if (index($$lines[$x+$z], '--'.$foo)==0) {
573                                         last;
574                                     }
575                                 }
576                                 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
577
578                                 @bo2 = @$lines[$x+$z..$#$lines];
579                                 splice(@$lines, $x+$z);
580                                 if ($$lines[$#$lines] =~ /^\s*$/) {
581                                     unshift(@bo2, pop @$lines);
582                                 }
583                                 @bo1 = @$lines[0..$x];
584                                 splice(@$lines, 0, $x+1);
585
586                                 # remove mailing list and ad footers within this
587                                 # attachment:
588                                 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
589                                 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
590
591                                 last;
592                             }
593                             else { 
594                                 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
595                                     ($hdrs?undef:\@hdr), $lines);
596                                 exit;
597                             }
598                         }
599                     }
600                 }
601                 last;
602             }
603         } 
604     }
605
606
607     # Protect verbatims:
608     $verb = 0;
609     for ($x=0; $x<scalar(@$lines); $x++) {
610         if ($$lines[$x] =~ /^\s*#v([+-])$/o) { 
611             $verb = $1 eq '+' ? 1 : 0;
612             $vrb[$x] = 1;
613         } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/o) {
614             # Detect and protect standard diffs.
615             # Skip and proceed to next line if premature file end or diff line
616             # numbers implausible (so it might be not a diff at all).
617             if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
618             $vrb[$x] = 1;
619             for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
620         } elsif ($diff &&
621                  $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/o) {
622             if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
623             $vrb[$x] = 1;
624             for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
625         } elsif ($diff &&
626                  $$lines[$x] =~ /^--- ./o &&
627                  $$lines[$x+1] =~ /^\+\+\+ ./o &&
628                  $$lines[$x+2] =~ /^@@ -[0-9]+,([0-9]+) \+[0-9]+,([0-9]+) @@/o
629                 ) {
630             # Detect and protect unified diffs.
631             # Proceed only as far as the diff should go.
632             my $minus = $1;
633             my $plus = $2;
634             $vrb[$x++] = 1;
635             $vrb[$x++] = 1;
636             $vrb[$x++] = 1;
637             for (my $cminus = 0, my $cplus = 0;
638                  $cminus<=$minus && $cplus<=$plus && $x<$#$lines;
639                  $x++) {
640                 $vrb[$x] = 1;
641                 if ($$lines[$x] !~ /^\+/) { $cminus++; };
642                 if ($$lines[$x] !~ /^-/) { $cplus++; };
643             }
644             $x-=1;
645         } else { $vrb[$x] = $verb; }
646     }
647
648     # Calculate quoting ratio (with respect to verbatims):
649     if ($check && scalar(@$lines)) {
650         my ($y, $z) = (0, 0);
651         for ($x=0; $x<scalar(@$lines); $x++) {
652             if (!$vrb[$x]) {
653                 $z++;
654                 if (index($$lines[$x], $indent)==0) { $y++; }
655             }
656         }
657
658         if ($y/$z>=$check_ratio) {
659             print $msg_ratio;
660             exit EX_UNAVAILABLE;
661         }
662     }
663
664     if ($mua eq 'mutt') {
665         # See if we find pgp output generated by mutt before we scramble
666         # the thing. If yes, see if we can beautify it.
667         if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
668
669         # Remove all but the first attachment (if this is text/plain)
670         # mutt did introduce (bah!). Remember, all this ugliness could
671         # be replaced with a proper and clean edit_filter patch in 
672         # mutt(1) itself...
673         for ($x=$#$lines; $x>=0; $x--) {
674             if ($vrb[$x]) { next; }
675             # The following regexp's are quite ugly because for most users
676             # these lines are coloured using termcap... (bah!)
677             if (($$lines[$x] =~
678                      /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
679                     (($1 ne '1') ||
680                     ($x<$#$lines &&
681                         $$lines[$x+1] !~ m!^
682                             (?:\e[^\a]+\a)?
683                             (?:\Q$mutt_contenttype\E)
684                             (?:text/plain|application/pgp)
685                         !oxi))) ||
686                 ($$lines[$x] =~ /^
687                     (?:\e[^\a]+\a)?
688                     (?:\Q$mutt_pgpsigned\E     |
689                        \Q$mutt_pgpclearsigned\E|
690                        \Q$mutt_pgpcryptend\E|
691                        \Q$mutt_pgpencrypted\E)
692                 /ox))
693             { 
694                 # Strip attachments to prepare further processing
695                 unshift(@att, @$lines[$x..$#$lines]);
696                 splice(@$lines, $x);
697                 # Try to fix trailing empty lines
698                 while (scalar(@$lines) && $$lines[$#$lines] =~
699                     /^(?:\e[^\a]+\a)?\s*$/) { 
700                     unshift(@att, pop(@$lines));
701                 }
702
703                 # Remove ML and ad footers within attachments:
704                 my @tmp;
705                 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
706                 if ($ad) { remove_footers($lines, \@tmp, $ads,     undef); }
707                 $x = scalar(@$lines);
708             }
709         }
710
711         # care about the rest
712         if (scalar(@att)) {
713             for ($x=0; $x<$#att; $x++) {
714                 if ($vrb[scalar(@$lines)+$x]) { next; }
715
716                 # Pipe message/rfc822 parts to another instance of
717                 # process_msg() for further processing.
718                 # Please note that we cannot see what a hierarchy the
719                 # original message had -- if there were message/rfc822
720                 # parts within other message/rfc822 parts constellations
721                 # can occur which we cannot resolve. Therefore we simply
722                 # do not even try to be smart. This should work for most
723                 # situations.
724                 # The following regexp is quite ugly because for most
725                 # users the line is coloured using termcap... (bah!)
726                 if ($att[$x] =~
727                         /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
728                     $att[$x+1] =~ m!^
729                         (?:\e[^\a]+\a)?
730                         (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
731                     !ox)
732                 {
733                     $x += 2;
734                     while ($att[$x] !~ /^\s*$/) { $x++; }
735                     $x++;
736
737                     my @tmp = @att[$x..$#att];
738                     process_msg(\@tmp);
739                     splice(@att, $x, scalar(@att)-$x, @tmp);
740                     $x += scalar(@tmp);
741                 }
742             }
743         }
744     }
745
746     # Remove signature:
747     if (scalar(@$lines)) { 
748         my $sn = 0;
749         my $chk_empty = 1;
750         my $empty = 0;
751
752         for ($x = $#$lines; $x>=0; $x--) {
753             if (!$vrb[$x]) {
754                 if ($$lines[$x] =~ /^-- $/) {
755                     if ($diff) {
756                         for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
757                             if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
758                                 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
759                             {
760                                 $sig = 0;
761                                 unshift(@sig, @$lines[$x..$#$lines]);
762                                 splice(@$lines, $x);
763                                 last;
764                             }
765                         }
766                         if (scalar(@sig)) {
767                             if (defined($sign) && ++$sn==$sign) { last; } else { next; }
768                         }
769                     }
770
771                     if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
772                         if ($lsig && !$sig) {
773                             unshift(@sig, "[---=| Overlong signature removed by $0: " .
774                                 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
775                         }
776                         splice(@$lines, $x);
777                     }
778                     else {
779                         unshift(@sig, @$lines[$x..$#$lines]);
780                         splice(@$lines, $x);
781                     }
782                     if (defined($sign) && ++$sn==$sign) { last; } else { next; }
783                 }
784                 # any trailing newlines?
785                 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
786                 elsif ($chk_empty) { $chk_empty = 0; }
787             }
788         }
789     }
790
791     # Fix quote markers to adhere to RFC 3676, this changes "> >" to ">> " (if
792     # the default $indent is used). The space after ">" is not mandatory by RFC
793     # but makes the result more readable.
794     if ($fixind) {
795         for ($x=0; $x<scalar(@$lines); $x++) {
796             if (!$vrb[$x] && index($$lines[$x], $indent)==0) {
797                 # We match space at the beginning to prevent removal of spaces
798                 # directly after the last quote mark.
799                 $$lines[$x] =~ /^((\Q$indent\E| )*\Q$indent\E ?)(.*)$/;
800                 my $tmp = $1;
801                 my $len = length $1;
802                 my $rest = $3;           # if anything follows after the quote
803                 $tmp =~ tr/ //d;
804                 $tmp .= ' ' if $rest;    # don't create trailing whitespace
805                 substr($$lines[$x], 0, $len, $tmp);
806             }
807         }
808     }
809
810     # See if there is some Kammquoting to fix:
811     if ($kamm) { decomb($lines, \@vrb); }
812
813     # Now care about TOFU.
814     # One common mispractice is M$ and Groupwise style TOFU:
815     if ($ms||$gw) {
816         # bloat this array if you want more internationalization:
817         my @tofu = ('Original Message',
818                     'Original-Nachricht',
819                     'Urspr.ngliche Nachricht',
820                     'Mensagem original',
821                     'Ursprungligt meddelande',
822                     'Oorspronkelijk bericht',
823                     'Message d\'origine',
824                     'Origin message',
825                     'Forwarded message',
826                     'Weitergeleitete Nachricht / Forwarded Message',
827                     '[A-Za-z ]+ <(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})> schrieb:');
828         my $k = 0;    # any text above?
829         my $tmp = 0;  # flagged if inside PGP or SSL output
830
831         DONE: for ($x=0; $x<scalar(@$lines); $x++) { 
832             if (!$vrb[$x]) {
833                 foreach my $tmp (@tofu) {
834                     if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
835                         ($gw &&
836                             ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
837                              $$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/))) { 
838                         $x++;
839                         $trad = 0;
840                         $bigqn = 0;
841                         last DONE; 
842                     }
843                 }
844
845                 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
846                     ((!$mua) ||
847                      ($mua eq 'mutt' &&
848                          # this line seems not necessary since mutt-1.5.18:
849                          $$lines[$x] !~ /^\e[^\a]+\a$/o &&
850                          $$lines[$x] !~
851                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
852                          $$lines[$x] !~
853                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
854                     ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
855                 {
856                     if ($mua eq 'mutt' && (!$tmp) &&
857                         ($$lines[$x] =~
858                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o ||
859                          $$lines[$x] =~
860                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_ssloutstart\E)/o)) {
861                         $tmp = 1;
862                     } elsif ($mua eq 'mutt' && $tmp && 
863                         ($$lines[$x] =~
864                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
865                          $$lines[$x] =~
866                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpcryptstart\E)/o ||
867                          $$lines[$x] =~
868                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
869                         $tmp = 0;
870                     } elsif (!$tmp) {
871                         $k = 1;
872                     }
873                 }
874             }
875         }
876
877         # try to avoid false positives and only delete m$ style tofu if
878         # there is text above
879         if ($k) {
880             if (!$ms_smart) { goto CLEAN; }
881
882             # first, see if there is pgp stuff inside the tofu:
883             my $p = 0;    # levels of pgp signed parts
884
885             for (my $i=$x+1; $i<scalar(@$lines); $i++) {
886                 if ($$lines[$i] =~
887                         /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
888                     $p++;
889                 }
890             }
891             if ($p) {
892                 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
893                     if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
894                         splice(@att, 0, $i);
895                         unshift(@att, "\n");
896                         goto CLEAN;
897                     } elsif ($att[$i] =~
898                             /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
899                         splice(@att, 0, $i+1);
900                         $p--;
901                         goto STAIRS;
902                     }
903                 }
904                 splice(@att);
905             }
906
907             # now removing is safe:
908             CLEAN: $j = scalar(@$lines)-$x;
909             splice(@$lines, $x);
910         }
911     }
912
913     # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
914     # much more aggressively, so we won't need to search any more if we
915     # did find some):
916     if ($trad && (!$j) && !$vrb[$#$lines]) {
917         if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) { 
918             unshift(@sig, pop(@$lines));
919         }
920
921         my $k;
922         my $x = 1;
923
924         for (my $i=$#$lines; $i>=0; $i--) {
925             if ($vrb[$i]) { last; }
926             if (index($$lines[$i], $indent)==0) {
927                 $j++;
928                 $k = $i;
929             }
930             elsif ($$lines[$i] !~ /^\s*$/) { last; }
931         }
932
933         if ($j) {
934             # if there is no text above, we will assume the message is meant
935             # as forwarding and therefore OK
936             for (my $i=$k-1; $i>=0; $i--) {
937                 if ($$lines[$i] !~ /^\s*$/o) {
938                    $x = 0;
939                    last;
940                 }
941             }
942             if ($x) {
943                 $j = 0;
944             } else {
945                 splice(@$lines, $k);
946             }
947         }
948     }
949
950     # OK, if we found TOFU, we will leave a message that we were here...
951     if ($j) { 
952         # make sendmail bounce if we shall be picky 
953         # and indeed found something:
954         if ($mda) { 
955             if ($mda ne '1') { 
956                 print STDERR $boun;
957
958                 if ($sysl) {
959                     eval { require Sys::Syslog; }; 
960                     if ($@) { warn $@; } else {
961                         Sys::Syslog::setlogsock('unix');
962                         Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
963                         Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
964                         Sys::Syslog::closelog();
965                     }
966                 }
967             }
968
969             exit EX_BOUNCE;
970         }
971
972         # if we were invoked just for checking and indeed found something,
973         # print out the error message and quit:
974         if ($check) {
975             print $msg_quote;
976             exit EX_UNAVAILABLE;
977         }
978
979         push(@$lines, "[---=| TOFU protection by $0: " .
980             "$j lines snipped |=---]\n");
981     }
982     elsif ($mda eq '1') { exit EX_OK; }
983
984     # Care for huge blocks of quoted original message:
985     if ($bigqn) { debigq($lines, \@vrb); }
986
987     # Care for trailing whitespaces:
988     if ($trsp) {
989         for ($x=0; $x<scalar(@$lines); $x++) { 
990             if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
991         }
992     }
993
994     # Care for punctuation abuse:
995     if ($elli) {
996         for ($x=0; $x<scalar(@$lines); $x++) { 
997             if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
998         }
999     }
1000
1001     # (Nearly) at last care for multiple blank lines. (Do not do this
1002     # earlier -- the way it is done right now would screw up the verbatim
1003     # list)
1004     if ($cr) {
1005         # When handling regular TOFU above we move the last empty line from
1006         # the body to the signature. This prevents the correct removal of
1007         # empty lines before a signature (one line less is removed than it
1008         # should) when -c and -t are used. This fixes it.
1009         if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
1010             push(@$lines, shift(@sig));
1011         }
1012
1013         my $t = 0;
1014         for ($x=scalar(@$lines)-1; $x>=0; $x--) {
1015             if ((!$vrb[$x]) &&
1016                 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
1017                   $$lines[$x] =~ /^\s*$/o))
1018             { 
1019                 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
1020             }
1021             else { $t = 0; }
1022         }
1023     }
1024
1025     # Everything changing the body is done now. Time to fix the line count
1026     # header so naive clients do not get confused. Just to be sure, append
1027     # the old line count to X-headers.
1028     my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
1029                 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) + 
1030                 (!$ad?scalar(@ads):0);
1031     if ($linecount-$l!=0) {
1032         for ($x=0; $x<scalar(@hdr); $x++) {
1033             if ($hdr[$x] =~ 
1034                 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1035             { 
1036                 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1037                 push(@hdr, "\n");
1038             }
1039         }
1040     }
1041
1042     # Finally, before leaving we put everything back in right order.
1043     unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1044     push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1045         @bo2);
1046 }
1047
1048
1049 # environment
1050 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1051                  ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1052
1053 # command line switches
1054 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1055     $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1056     $sani, $sig, $sigint, $spass, $trad, $trsp) =
1057     (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);
1058 my $ifile   = '-';    # use STDIN if nothing specified
1059
1060 # temp vals:
1061 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1062     $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1063
1064 # get command line params:
1065 if (!Getopt::Long::GetOptions(
1066     'a'             => \$ad,
1067     'A=s'           => \$ads,
1068     'bigq:s'        => \$_t_bigq,
1069     'body'          => \$_t_nohdr,
1070     'c:i'           => \$_t_cr,
1071     'check:s'       => \$_t_check,
1072     'debug|d'       => \$sysl,
1073     'diff'          => \$diff,
1074     'e'             => \$elli,
1075     'fixind'        => \$fixind,
1076     'ftr-ad'        => \$ftr_ad,
1077     'ftr-ml'        => \$ftr_ml,
1078     'groupwise'     => \$gw,
1079     'help|h'        => \$_t_help,
1080     'i=s'           => \$ifile,
1081     'k'             => \$kamm,
1082     'kminl=i'       => \$_t_kminl,
1083     'kmaxl=i'       => \$_t_kmaxl,
1084     'kdiff=i'       => \$_t_kdiff,
1085     'L=s'           => \$footers,
1086     'l'             => \$ml,
1087     'lax-security'  => \$lax,
1088     'locale=s'      => \$locale,
1089     'max-lines:i'   => \$maxlines,
1090     'ms-smart'      => \$ms_smart,
1091     'mua|M=s'       => \$_t_mua,
1092     'm'             => \$ms,
1093     'o=s'           => \$ofile,
1094     'P=s'           => \$boun,
1095     'p:s'           => \$_t_redir,
1096     'pgp-short'     => \$pgpshort,
1097     'pgp-move'      => \$pgpmove,
1098     'pgp-move-vrf'  => \$pgpmovevrf,
1099     'r'             => \$hdrs,
1100     'reply'         => \$reply,
1101     'S:i'           => \$_t_maxsig,
1102     's'             => \$sig,
1103     'sani'          => \$sani,
1104     'sigsmax:i'     => \$_t_sigsmax,
1105     'spass'         => \$spass,
1106     't'             => \$trad,
1107     'version|v'     => \$_t_ver,
1108     'w'             => \$trsp
1109     )) {
1110     help();
1111 }
1112
1113 # clean up temp vals:
1114 if (defined $_t_bigq) {
1115     if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1116     $bigqn = $1?$1:30; 
1117     $bigqx = $2?$2:10; 
1118     if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1119 }
1120 if (defined $_t_help) { help(); }
1121 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1122 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1123 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1124 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1125 if (defined $_t_mua) {
1126     $mua = lc($_t_mua);
1127     # mutt still displays the message when ^C'ing pgp verification:
1128     if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1129 }
1130 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1131 if (defined $_t_check) {
1132     $check = 1;
1133     while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1134         my $foo = $1;
1135         $_t_check = $2;
1136         if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1137     }
1138 }
1139 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1140 if (defined $_t_ver) { version(); }
1141 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1142 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1143 if ($ms_smart) { $ms = 1; }
1144
1145 if ($mua eq 'mutt') {
1146     if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1147         eval { require Locale::gettext; };
1148         if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1149             Locale::gettext::textdomain('mutt');
1150             ($mutt_attachment) =
1151                 Locale::gettext::gettext("[-- Attachment #%d") =~
1152                 m/^([^%]*)/;
1153             ($mutt_contenttype) =
1154                 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1155                 m/^([^%]*)/;
1156             ($mutt_pgpsigned) =
1157                 Locale::gettext::gettext("\n[-- End of signed data --]\n")  =~
1158                 m/^\n*(.*)\n/m;
1159             ($mutt_beginsigned) =
1160                 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1161                 m/^(.*)\n/m;
1162             ($mutt_pgpclearsigned) =
1163                 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1164                 m/^(.*)\n/m;
1165             ($mutt_pgpclearsigstart) =
1166                 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n\n") =~
1167                 m/^(.*)\n/m;
1168             ($mutt_pgpencrypted) =
1169                 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1170                 m/^(.*)\n/m;
1171             ($mutt_pgpoutstart) =
1172                 sprintf(Locale::gettext::gettext("[-- %s output follows%s --]\n"), 'PGP', '%s') =~
1173                 m/^(.*)%s/;
1174             $mutt_pgpoutstart =~ s/%s/PGP/;
1175             ($mutt_pgpoutend) =
1176                 Locale::gettext::gettext("[-- End of PGP output --]\n\n") =~
1177                 m/^(.*)\n/m;
1178             ($mutt_ssloutstart) =
1179                 Locale::gettext::gettext("[-- %s output follows%s --]\n") =~
1180                 m/^(.*)%s/;
1181             $mutt_ssloutstart =~ s/%s/OpenSSL/;
1182             ($mutt_ssloutend) =
1183                 Locale::gettext::gettext("[-- End of OpenSSL output --]\n\n") =~
1184                 m/^(.*)\n/m;
1185
1186             Locale::gettext::textdomain('gnupg');
1187             ($gpg_WARNING) =
1188                 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1189                 m/^([^:]*: )/;
1190             ($gpg_Warning) =
1191                 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1192                 m/^([^:]*: )/;
1193             ($gpg_Cantcheck) =
1194                 Locale::gettext::gettext("Can't check signature: %s\n") =~
1195                 m/^([^%]*)/;
1196             ($gpg_aka) =
1197                 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1198                 m/^([^"«%]*)["«%]/;
1199             ($gpg_bad) =
1200                 Locale::gettext::gettext('BAD signature from "%s"') =~
1201                 m/^([^"«%]*)["«%]/;
1202             ($gpg_expired) =
1203                 Locale::gettext::gettext("Note: This key has expired!\n") =~
1204                 m/^(.*)\n/m;
1205             ($gpg_good) =
1206                 Locale::gettext::gettext('Good signature from "%s"') =~
1207                 m/^(([^"«%]*))["«%]/;
1208             ($gpg_bug) =
1209                 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1210                 m/^([^%]*)/;
1211         }
1212     }
1213
1214 }
1215 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1216  
1217 if (($ml && $footers eq '')||    # no -l without -L
1218     ($ad && $ads eq '')||        # no -a without -A
1219     ($nohdr && $pgpshort)||      # --body and --pgp-short are like oil and water
1220     (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1221     ($ifile eq '')||             # no empty -i
1222     ($ofile eq ''))              # no empty -o
1223 { help(); }
1224
1225
1226 # Read message:
1227 if (!open(IN, $ifile))
1228     { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1229 my @message = <IN>;
1230 close IN;
1231
1232 # First, check msg length and stop processing if msg is too long:
1233 if ((defined $maxlines) && (@message > $maxlines)) {
1234     if ($mua eq 'mutt') {
1235         my $x = 0;
1236         if (!$nohdr) {
1237             do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1238             $x++;
1239         }
1240         splice(@message, $x, 0,
1241             ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1242     }
1243     write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1244
1245     if ($mua eq 'mutt') { exit(EX_OK); }
1246     exit(EX_DATAERR);
1247 }
1248
1249 # this should be self-explanatory:
1250 process_msg(\@message);
1251
1252 # Finally, print clean lines:
1253 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1254
1255 # vim600:set foldmethod=marker:
1256 # eof