]> git.deb.at Git - pkg/t-prot.git/blob - t-prot
Merge commit 'upstream/2.99'
[pkg/t-prot.git] / t-prot
1 #!/usr/bin/perl -w
2 # $Id: t-prot,v 1.307 2010/07/01 16:02:56 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            => '2.99';
9 use constant REV            => '';
10 use constant REL            => q$Revision: 1.307 $=~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
36 );
37
38
39 # Please adjust these vals to your needs only if you know what you are
40 # doing. They are no constants because command line can change them or
41 # they are used in some rexexp.
42 $0 =~ s!^.*/!!;
43 $maxsig         = 4;      # max. valid signature length
44 $maxlines       = undef;  # no limit of message lines
45 $crshrink       = 2;      # multiple blank lines are shrunk to $crshrink lines
46 $indent         = '>';    # Indent string to identify a quoted line
47 $kminl          = 65;     # see decomb() for details
48 $kmaxl          = 80;
49 $kdiff          = 20;
50 $pgpshort       = 0;      # hide pgp key ids if set
51 $pgpmove        = 0;      # move pgp output to bottom if set
52 $pgpmovevrf     = 0;
53 $sign           = 1;      # max number of sigs tolerated, undef for no limit
54 $boun           = "Blocked by $0: This user does not accept TOFUed email. Please see <http://learn.to/quote/> and <http://www.escape.de/users/tolot/mutt/> for more info. Have a nice day!\n";
55 $ftr_ad         = undef;  # too hard on performance to be default
56 $ftr_ml         = undef;  # too hard on performance to be default
57 $ofile          = '-';    # use STDOUT if nothing is specified
58 $spass_prefix   = 'SPAM: ';
59 $check_ratio    = 0;      # off by default
60 $msg_quote      = "Blocked by $0: This message has been rejected because of a full quote. Please see http://learn.to/quote/ and http://www.escape.de/users/tolot/mutt/ for more info. Have a nice day!\n";
61 $msg_ratio      = "Blocked by $0: This message has been rejected because of excessive quoting. Please see http://learn.to/quote/ and http://www.escape.de/users/tolot/mutt/ for more info. Have a nice day!\n";
62 # end of user adjusted vals
63
64
65 # set the defaults to the C locale
66 $mutt_attachment        = '[-- Attachment #';
67 $mutt_contenttype       = '[-- Type: ';
68 $mutt_pgpsigned         = '[-- End of signed data --]';
69 $mutt_beginsigned       = '[-- The following data is signed --]';
70 $mutt_pgpclearsigstart  = '[-- BEGIN PGP SIGNED MESSAGE --]';
71 $mutt_pgpclearsigned    = '[-- END PGP SIGNED MESSAGE --]';
72 $mutt_pgpencrypted      = '[-- End of PGP/MIME encrypted data --]';
73 $mutt_pgpoutstart       = '[-- PGP output follows (current time:';
74 $mutt_pgpoutend         = '[-- End of PGP output --]';
75
76 # set the defaults to the C locale
77 $gpg_WARNING            = 'WARNING: ';
78 # (yes, the translations in gnupg's po files *are* braindamaged):
79 $gpg_Warning            = 'WARNING: ';
80 $gpg_Cantcheck          = 'Can\'t check signature: ';
81 $gpg_aka                = '                aka ';
82 $gpg_bad                = 'BAD signature from ';
83 $gpg_expired            = 'Note: This key has expired!';
84 $gpg_good               = 'Good signature from';
85 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
86 $gpg_bug                = '... this is a bug (';
87
88
89 # help(): print help text and exit with appropriate exit code
90 sub help {
91     print "Usage: $0 [options] 
92   -A DIRECTORY    ad footer directory, treat ad footers as signature
93   -a              remove ad footers; requires -A
94   --bigq[=n[,x]]  remove all but x lines of quotes with more than n
95                   lines; default is n=30 and x=10
96   --body          input has no headers; does not work with --pgp-short;
97                   multipart messages will not be detected
98   -c[n]           merge multiple blank lines to n lines (default is 2)
99   --check[=FLAGS] check various criteria, print error message and quit;
100                   see man page for details
101   -d, --debug     print notice to syslog when bouncing; requires -p
102   --diff          tolerate diffs
103   -e              force ellipsis for excessive punctuation
104   --fixind        fix quotes to adhere to RFC 3676
105   --ftr-ad        enable aggressive ad footer matching; requires -A
106   --ftr-ml        enable aggressive mailing list footer matching; req. -L
107   --groupwise     delete Novell Groupwise style TOFU
108   -h, --help      show this short help and exit
109   -i INFILE       file to be read; '-' for STDIN (default)
110   -k              try to fix \"Kammquotes\"
111   --kdiff=n       max. length difference between wrapped lines; req. -k
112   --kmaxl=n       max. line length for wrapped line; requires -k
113   --kminl=n       min. line length for wrapped line; requires -k
114   -L DIRECTORY    mailing list footer directory, treat mailing list
115                   footers as signature
116   -l              delete mailing list footer; requires -L
117   --lax-security  use unsafe writing method; USE ON YOUR OWN RISK!
118   --locale=LOCALE internationalization; currently only used with -Mmutt
119   -M, --mua=MUA   turn on special treatment for some mail user agents
120   -m              delete MS style TOFU; careful: might be too aggressive
121   --max-lines=x   maximum number of message lines
122   --ms-smart      try to be smart with MS style TOFU; req. -Mmutt and -m
123   -o OUTFILE      file to be written to; '-' for STDOUT (default)
124   -P MESSAGE      user defined bounce message; requires -p
125   -p[ADDRESS]     redirect to ADDRESS if no TOFU was found
126   --pgp-move      move pgp verification output to bottom; requires -Mmutt
127   --pgp-move-vrf  move pgp output if verified and good; requires -Mmutt
128   --pgp-short     hide non-relevant pgp key uids; requires -Mmutt
129   -r              delete mail header lines
130   --reply         squeeze multiple reply prefixes in subject line
131   -S[n]           suppress signatures with more than n lines (default $maxsig)
132   -s              delete signature
133   --sani          sanitize some header fields
134   --sigsmax[=n]   max number of sigs tolerated, no value for unlimited
135   --spass         enable SpamAssassin workaround
136   -t              delete traditional style TOFU
137   -v, --version   show version string and exit
138   -w              delete trailing whitespaces\n";
139     exit(EX_USAGE);
140 }
141
142 # version(): print version info and exit with appropriate exit code
143 sub version {
144     print "$0 v".VER.REV." (Rev. ".REL."), Jochen Striepe <t-prot\@tolot.escape.de>
145 Get the latest version at <http://www.escape.de/users/tolot/mutt/>\n";
146     exit(EX_OK);
147 }
148
149 # sigint_handler(): what to do if we receive a single SIGINT
150 sub sigint_handler {
151     $sigint = 1;
152 }
153
154 sub sanitize_hdr {
155     # Undoes MIME quoted-printable word encoding.
156     sub qp_decode {
157         my $word = shift;
158         $word =~ tr/_/\x20/;
159         $word =~ s/=DF/ss/og;
160         $word =~ s/=C4/Ae/og;
161         $word =~ s/=D6/Oe/og;
162         $word =~ s/=DC/Ue/og;
163         $word =~ s/=E4/ae/og;
164         $word =~ s/=F6/oe/og;
165         $word =~ s/=FC/ue/og;
166         $word =~ s/=([0-9A-F]{2})/chr(hex $1)/ioge;
167         $word;
168     }
169
170     sub umlauts {
171         my $word = shift;
172         $word =~ s/ä/ae/og;
173         $word =~ s/Ä/Ae/og;
174         $word =~ s/ö/oe/og;
175         $word =~ s/Ö/Oe/og;
176         $word =~ s/ü/ue/og;
177         $word =~ s/Ü/Ue/og;
178         $word =~ s/ß/ss/og;
179         $word;
180     }
181
182     my $H = shift;        # array of header lines
183     my $i;
184
185     for ($i=0; $i<$#$H; $i++) {
186         if ($$H[$i] =~ /^subject: /io) {
187             if ($sani) {
188                 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
189                 $$H[$i] =~ s/^subject: *(.*)/"Subject: " . umlauts($1)/ioe;
190             }
191             if ($reply) {
192                 $$H[$i] =~ s/^subject: *(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/Subject: Re: /io;
193             }
194
195             $i++;
196             while ($i<$#$H && $$H[$i] =~ /^\s/) {
197                 if ($sani) {
198                     $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
199                     $$H[$i] =~ s/^\s+(\S.*)/" " . umlauts($1)/ioe;
200                 }
201                 if ($reply) {
202                     $$H[$i] =~ s/^\s+(?:(?:Re|Antw(?:ort)?|AW|WG): *)+/ /io;
203                 }
204                 $i++;
205             }
206         }
207         elsif ($sani && $$H[$i] =~ /^(?:from|to): /io) {
208             $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
209
210             $i++;
211             while ($i<$#$H && $$H[$i] =~ /^\s/) {
212                 $$H[$i] =~ s/=\?iso-8859-15?\?q\?([^?]*)\?=/qp_decode($1)/ioge;
213                 $i++;
214             }
215         }
216     }
217 }
218
219 # remove_footers(): remove any trailing appearance of footers contained
220 # in the given directory.
221 sub remove_footers {
222     my $L = shift;        # array of message lines
223     my $S = shift;        # array to store removed lines in
224     my $F = shift;        # footers dir name
225     my $O = shift;        # remove only one footer?
226     my $V = shift;        # allow footers match before end of message
227     my $off;
228
229     if (!defined $V) {
230         for ($off = 0; $#$L>=$off && $$L[$#$L-$off] =~ /^\s*$/; $off++) {;};
231     }
232
233     if ($F && scalar(@$L)) {
234         if (!opendir(DIR, $F))
235             { print STDERR "Could not open $F: $!\n"; exit(EX_IOERR); }
236         my @feet = grep { /^[^.]/ && -f "$F/$_" } readdir DIR;
237         closedir DIR;
238
239         foreach my $f (@feet) {
240             if (!open(IN, "$F/$f"))
241                 { print STDERR "Could not open $F/$f: $!\n"; exit(EX_IOERR); }
242             my @l = <IN>;
243             close IN;
244
245             if (!scalar(@l)) { next; }
246             for (my $z=0; $z<=$#l; $z++) { chomp($l[$z]); }
247
248             if (defined $V) {
249                 WIPE: for (my $z=scalar(@$L)-scalar(@l); $z>=0; $z--) {
250                     if (scalar(@l)+$z<=scalar(@$L)) {
251                         my $y = 0;
252                         for(my $x=1; $x<=scalar(@l); $x++) {
253                             if (index($$L[scalar(@$L)-$x-$z],
254                                           $l[scalar(@l)-$x])!=0) {
255                                 $y = 1;
256                             }
257                         }
258                         if (!$y) {
259                             unshift(@$S, @$L[$#$L-$#l-$z..$#$L]);
260                             splice(@$L, $#$L-$#l-$z);
261                             while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
262                                 unshift(@$S, pop(@$L));
263                             }
264                             if ($O) { last; } else { goto WIPE; }
265                         }
266                     }
267                 }
268             }
269             else {
270                 while (scalar(@l)<=scalar(@$L)) {
271                     for(my $x=1; $x<=scalar(@l); $x++) {
272                         if (index($$L[scalar(@$L)-$x-$off], $l[scalar(@l)-$x])!=0) { 
273                             goto FINISH;
274                         }
275                     }
276                     unshift(@$S, @$L[$#$L-$off-$#l..$#$L]);
277                     splice(@$L, $#$L-$off-$#l);
278                     while (scalar(@$L) && $$L[$#$L] =~ /^\s*$/) {
279                         unshift(@$S, pop(@$L));
280                     }
281                     if ($O) { last; }
282                 }
283                 FINISH:
284             }
285         }
286     }
287 }
288
289 # decomb(): Try to detect and fix zig-zag shaped quoting (a.k.a. German
290 # "Kammquoting").
291 sub decomb {
292     my $L = shift;        # array of message lines
293     my $V = shift;        # array with verbatim list
294     my $max = 0;        # plausible wraparound pos
295
296     # We scan the whole message first for a plausible common maximum line
297     # length where longer lines would be wrapped.
298     for (my $x=0; $x<$#$L; $x++) {
299         if ($$V[$x]!=1 && $max<length($$L[$x])) { $max = length($$L[$x]); }
300     }
301
302     # Next we see if there are plausible wraparounds.    
303     for (my $x=0; $x+1<$#$L; $x++) {
304
305         # OK, 
306         # * it must not be verbatim,
307         # * the 2nd line must not be quoted nor empty nor just 
308         #   underlining some part of the line above (using '^')
309         #   nor begin with a whitespace,
310         # * the 1st line must not end with a hyphen,
311         # * the 2nd line must not indicate content was deleted,
312         # * the 2nd line must not be some mutt(1) commentary,
313         # * there must not be a valid word wrap to produce a longer
314         #   1st line (if not quoted),
315         # * the 1st and 2nd line together must not be longer than some
316         #   magical upper limit nor shorter than some magical lower
317         #   bound nor nearly of the same length, and
318         # * the 3rd line must not be empty.
319         # With all that odds should be quite good that we have an
320         # automatedly wrapped line. Please send a note if you have
321         # additional good criteria. Thanks.
322         if (($$V[$x]!=1 && $$V[$x+1]!=1) &&
323             (index($$L[$x+1], $indent)!=0) &&
324             ($$L[$x+1] !~ /^$/) &&
325             ($$L[$x+1] !~ /^[\s^]/) &&
326             ($$L[$x]   !~ /-$/) &&
327             ($$L[$x+1] !~ /^\Q[...]\E\s*$/) &&
328             ($mua ne 'mutt' || $$L[$x+1] !~ /^(?:\e[^\a]+\a)?\[-- /) &&
329             (length($$L[$x])+index($$L[$x+1], ' ')>$max ||
330                 (index($$L[$x+1], ' ')<0 &&
331                  length($$L[$x])+length($$L[$x+1])>$max)) &&
332             (length($$L[$x])+length($$L[$x+1])<$kmaxl) &&
333             (length($$L[$x])+length($$L[$x+1])>$kminl) &&
334             (length($$L[$x])-length($$L[$x+1])>$kdiff) &&
335             ($$L[$x+2] !~ /^\s*$/))
336         {
337             $$L[$x] =~ s/\s*$/' ' . $$L[$x+1]/e;
338             splice(@$L, $x+1, 1);
339             splice(@$V, $x+1, 1);
340         }
341     }
342 }
343
344 # debigq(): Finds big quotes (more than $n lines quoted) and deletes all
345 # but the last $x lines of them.
346 sub debigq {
347     my $L = shift;        # array of message lines
348     my $V = shift;        # array with verbatim list
349     my $k = 0;
350
351     for (my $i=0; $i<=$#$L; $i++) {
352
353         if ($$V[$i]) { 
354             $k = 0;
355             next;
356         }
357
358         if (index($$L[$i], $indent)==0) { $k++; } else { 
359             if ($k>$bigqn) {
360                 my $x = $k-$bigqx;
361                 $i -= $k;
362
363                 $$L[$i] = "[---=| Quote block shrunk by $0: " .
364                     "$x lines snipped |=---]\n";
365                 $i++;
366                 splice(@$L, $i, $x-1);
367                 splice(@$V, $i, $x-1);
368
369                 $i++;
370             }
371             $k = 0;
372         }
373     }
374 }
375
376 # pgp(): treat mutt(1)'s pgp/gpg output contained in signed or encrypted
377 # messages
378 sub pgp {
379
380     sub verified {
381         my $L = shift;    # message body
382         my $X = shift;    # start line
383         my $Z = shift;    # end line
384
385                 my $ok = 0;
386
387         while ($X<$Z) {
388             if (index($$L[$X], "gpg: $gpg_WARNING")==0 ||
389                 index($$L[$X], "gpg: $gpg_Warning")==0 ||
390                 index($$L[$X], "gpg: $gpg_bad")==0 ||
391                 index($$L[$X], "gpg: $gpg_Cantcheck")==0 ||
392                 index($$L[$X], "gpg: $gpg_expired")==0 ||
393                 index($$L[$X], "gpg: $gpg_bug")==0)
394                 { return 0; }
395             if (index($$L[$X], "gpg: $gpg_good")==0)
396                 { $ok = 1; }
397             $X++;
398         }
399
400         return $ok;
401     }
402
403     my $L = shift;        # message body
404     my $V = shift;        # verbatim list
405     my $H = shift;        # headers
406
407     my @tmp = ();
408     my $tmp = 0;
409
410     for (my $x=0; $x<scalar(@$L); $x++) {
411         if ($$V[$x]) { next; }
412
413         if ($$L[$x]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o)
414         {
415             my $from;
416             for (my $m=0; $m<scalar(@$H); $m++) {
417                 if (index($$H[$m], 'From:')==0) {
418                     $from = $$H[$m];
419                     $m++;
420                     while (exists($$H[$m]) && $$H[$m] =~ /^\s/)
421                         { $from .= $$H[$m]; $m++; }
422                     last;
423                 }
424             }
425             ($from) = $from=~m/(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})/;
426
427             my $uid = 1;
428
429             for (my $i=$x+1; $i<scalar(@$L); $i++) {
430                 if ($pgpshort && index($$L[$i], "gpg: $gpg_aka")==0) {
431                     $uid++;
432
433                     if ($uid>1 && index($$L[$i], $from)<0) { 
434                         splice(@$L, $i, 1);
435                         splice(@$V, $i, 1);
436                         $i--;
437                     }
438                 }
439                 elsif ($$L[$i]=~/^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutend\E)/o) {
440                     if ($pgpmove ||
441                         ($pgpmovevrf && (!$sigint) && verified($L, $x+1, $i)))
442                     {
443                         push(@{$tmp[++$tmp]}, "\n", @$L[$x..($i+1)]);
444                         splice(@$L, $x, $i-$x+2);
445                         splice(@$V, $x, $i-$x+2);
446                         $i -= $#{$tmp[$tmp]}-2;
447                     }
448                     $x = $i;
449                     last;
450                 }
451             }
452         }
453         elsif ($tmp &&
454             $$L[$x] =~ /^
455                         (?:\e[^\a]+\a)?
456                         (?:\Q$mutt_pgpencrypted\E  |
457                            \Q$mutt_pgpclearsigned\E|
458                            \Q$mutt_pgpsigned\E)
459                        /ox)
460         {
461             splice(@$L, $x+1, 0, @{$tmp[$tmp]});
462             for (my $i=$x; $i<scalar(@{$tmp[$tmp]}); $i++) {
463                 splice(@$V, $x+1, 0, (0));
464             }
465             $x += scalar(@{$tmp[$tmp--]});
466             pop(@tmp);
467         }
468     }
469
470     while ($tmp>0) {
471         push(@$L, @{$tmp[$tmp--]});
472         pop(@tmp);
473         for (my $i=$#$V; $i<$#$L; $i++) { push(@$V, 0); }
474     }
475 }
476
477 # write_msg(): output
478 sub write_msg {
479     my $O = shift;
480     my $l;
481
482     if ((!$lax) && ($O =~ /^>(.*)/) && ($1 ne '-') && ($1 ne '/dev/null')) {
483         if (!sysopen(OUT, $1, O_EXCL|O_CREAT|O_WRONLY)) { 
484             print STDERR "Could not open $1: $!\n"; exit(EX_IOERR);
485         }
486     }
487     elsif (!open(OUT, $O)) { 
488         print STDERR "Could not open $O: $!\n"; exit(EX_IOERR);
489     }
490     while (scalar(@_)) {
491         $l = shift;
492         if (defined $l) {
493             $^W = 0;
494             print OUT @$l;
495             $^W = 1;
496         }
497     }
498     close OUT;
499 }
500
501 # process_msg(): This one proc does *everything* what has to be done with
502 # the lines of the message
503 sub process_msg {
504     my $lines = shift;
505
506     my ($j, $x, $verb) = (0, 0, 0);
507     my (@ads, @hdr, @bo1, @bo2, @ftr, @sig, @vrb, @att) = 
508         ((), (), (), (), (), (), (), (), ());
509
510     # Remove and store lines we might need later...
511     # Remove headers:
512     if (!$nohdr) {
513         for ($x=0; $x<$#$lines; $x++) { if (@$lines[$x] =~ /^$/) { last; }; }
514         @hdr = @$lines[0..$x];
515         splice(@$lines, 0, $x+1);
516     }
517     # remember the original body lines count
518     my $linecount = scalar(@$lines);
519
520     # Sanitize header fields:
521     if ($reply || $sani) { sanitize_hdr(\@hdr); }
522
523     # Remove ML footers:
524     remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
525
526     # Remove ad footers:
527     remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
528
529     # See if we have a multipart content type. If yes, see if it is already
530     # ripped (e.g. by you MUA, assuming it does not get special treatment with
531     # --mua=foo), otherwise only leave the first part if it is plain text (if
532     # not, we are done - non-text messages are not our business).
533     if ($mua ne 'mutt') { 
534         for ($x=0; $x<scalar(@hdr); $x++) {
535             if ($hdr[$x] =~ /^Content-[Tt]ype:\s+(.*)$/) {
536                 my $foo = $1;
537
538                 if ($foo =~ m!^multipart/!) {
539                     undef $foo;
540
541                     if ($hdr[$x] =~ /\Wboundary="([^"]+)"/i) { $foo = $1; }
542                     else { 
543                         for (my $z=1; $x+$z<@hdr && $hdr[$x+$z]=~/^\s/; $z++) {
544                             if ($hdr[$x] =~ /\Wboundary="?([^"\s]+)"?$/i) { 
545                                 $foo = $1;
546                                 last;
547                             }
548                         }
549                     }
550
551                     if (defined $foo) {
552                         for (my $x=0; $x<scalar(@$lines); $x++) {
553                             if (index($$lines[$x], '--'.$foo)!=0) { next; }
554
555                             my $bar = 'text/plain';
556                             for ($x++; $x<@$lines && $$lines[$x]!~/^$/; $x++)
557                             {
558                                 if ($$lines[$x] =~ /^Content-[Tt]ype:\s+(.*)/) { 
559                                     $bar = $1;
560                                 }
561                             }
562                             if ($x>=scalar(@$lines)) { exit(EX_DATAERR); }
563
564                             if ($bar =~ m!^text/plain!) {
565                                 my $z;
566                                 for ($z=1; $x+$z<@$lines; $z++) {
567                                     if (index($$lines[$x+$z], '--'.$foo)==0) {
568                                         last;
569                                     }
570                                 }
571                                 if ($x+$z>=scalar(@$lines)) { exit(EX_DATAERR); }
572
573                                 @bo2 = @$lines[$x+$z..$#$lines];
574                                 splice(@$lines, $x+$z);
575                                 if ($$lines[$#$lines] =~ /^\s*$/) {
576                                     unshift(@bo2, pop @$lines);
577                                 }
578                                 @bo1 = @$lines[0..$x];
579                                 splice(@$lines, 0, $x+1);
580
581                                 # remove mailing list and ad footers within this
582                                 # attachment:
583                                 remove_footers($lines, \@ftr, $footers, undef, $ftr_ml);
584                                 remove_footers($lines, \@ads, $ads, undef, $ftr_ad);
585
586                                 last;
587                             }
588                             else { 
589                                 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"),
590                                     ($hdrs?undef:\@hdr), $lines);
591                                 exit;
592                             }
593                         }
594                     }
595                 }
596                 last;
597             }
598         } 
599     }
600
601
602     # Protect verbatims:
603     $verb = 0;
604     for ($x=0; $x<scalar(@$lines); $x++) {
605         if ($$lines[$x] =~ /^\s*#v([+-])$/) { 
606             $verb = $1 eq '+' ? 1 : 0;
607             $vrb[$x] = 1;
608         } elsif ($diff && $$lines[$x] =~ /^[0-9]+a([0-9]+),([0-9]+)$/) {
609             # Detect and protect standard diffs.
610             # Skip and proceed to next line if premature file end or diff line
611             # numbers implausible (so it might be not a diff at all).
612             if ($1>$2 || $x+$2-$1+1>$#$lines) { next; }
613             $vrb[$x] = 1;
614             for (my $i=0; $i<$2-$1+1; $i++) { $vrb[++$x] = 1; }
615         } elsif ($diff && $$lines[$x] =~ /^([0-9]+),([0-9]+)c([0-9]+),([0-9]+)$/) {
616             if ($1!=$3 || $1>$2 || $3>$4 || $x+$2-$1+$4-$3+3>$#$lines) { next; }
617             $vrb[$x] = 1;
618             for (my $i=0; $i<$2-$1+$4-$3+3; $i++) { $vrb[++$x] = 1; }
619         } else { $vrb[$x] = $verb; }
620     }
621
622     # Calculate quoting ratio (with respect to verbatims):
623     if ($check && scalar(@$lines)) {
624         my ($y, $z) = (0, 0);
625         for ($x=0; $x<scalar(@$lines); $x++) {
626             if (!$vrb[$x]) {
627                 $z++;
628                 if (index($$lines[$x], $indent)==0) { $y++; }
629             }
630         }
631
632         if ($y/$z>=$check_ratio) {
633             print $msg_ratio;
634             exit EX_UNAVAILABLE;
635         }
636     }
637
638     if ($mua eq 'mutt') {
639         # See if we find pgp output generated by mutt before we scramble
640         # the thing. If yes, see if we can beautify it.
641         if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
642
643         # Remove all but the first attachment (if this is text/plain)
644         # mutt did introduce (bah!). Remember, all this ugliness could
645         # be replaced with a proper and clean edit_filter patch in 
646         # mutt(1) itself...
647         for ($x=$#$lines; $x>=0; $x--) {
648             if ($vrb[$x]) { next; }
649             # The following regexp's are quite ugly because for most users
650             # these lines are coloured using termcap... (bah!)
651             if (($$lines[$x] =~
652                      /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
653                     (($1 ne '1') ||
654                     ($x<$#$lines &&
655                         $$lines[$x+1] !~ m!^
656                             (?:\e[^\a]+\a)?
657                             (?:\Q$mutt_contenttype\E)
658                             (?:text/plain|application/pgp)
659                         !ox))) ||
660                 ($$lines[$x] =~ /^
661                     (?:\e[^\a]+\a)?
662                     (?:\Q$mutt_pgpsigned\E     |
663                        \Q$mutt_pgpclearsigned\E|
664                        \Q$mutt_pgpencrypted\E)
665                 /ox))
666             { 
667                 # Strip attachments to prepare further processing
668                 unshift(@att, @$lines[$x..$#$lines]);
669                 splice(@$lines, $x);
670                 # Try to fix trailing empty lines
671                 while (scalar(@$lines) && $$lines[$#$lines] =~
672                     /^(?:\e[^\a]+\a)?\s*$/) { 
673                     unshift(@att, pop(@$lines));
674                 }
675
676                 # Remove ML and ad footers within attachments:
677                 my @tmp;
678                 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
679                 if ($ad) { remove_footers($lines, \@tmp, $ads,     undef); }
680                 $x = scalar(@$lines);
681             }
682         }
683
684         # care about the rest
685         if (scalar(@att)) {
686             for ($x=0; $x<$#att; $x++) {
687                 if ($vrb[scalar(@$lines)+$x]) { next; }
688
689                 # Pipe message/rfc822 parts to another instance of
690                 # process_msg() for further processing.
691                 # Please note that we cannot see what a hierarchy the
692                 # original message had -- if there were message/rfc822
693                 # parts within other message/rfc822 parts constellations
694                 # can occur which we cannot resolve. Therefore we simply
695                 # do not even try to be smart. This should work for most
696                 # situations.
697                 # The following regexp is quite ugly because for most
698                 # users the line is coloured using termcap... (bah!)
699                 if ($att[$x] =~
700                         /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
701                     $att[$x+1] =~ m!^
702                         (?:\e[^\a]+\a)?
703                         (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
704                     !ox)
705                 {
706                     $x += 2;
707                     while ($att[$x] !~ /^\s*$/) { $x++; }
708                     $x++;
709
710                     my @tmp = @att[$x..$#att];
711                     process_msg(\@tmp);
712                     splice(@att, $x, scalar(@att)-$x, @tmp);
713                     $x += scalar(@tmp);
714                 }
715             }
716         }
717     }
718
719     # Remove signature:
720     if (scalar(@$lines)) { 
721         my $sn = 0;
722         my $chk_empty = 1;
723         my $empty = 0;
724
725         for ($x = $#$lines; $x>=0; $x--) {
726             if (!$vrb[$x]) {
727                 if ($$lines[$x] =~ /^-- $/) {
728                     if ($diff) {
729                         for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
730                             if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
731                                 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
732                             {
733                                 $sig = 0;
734                                 unshift(@sig, @$lines[$x..$#$lines]);
735                                 splice(@$lines, $x);
736                                 last;
737                             }
738                         }
739                         if (scalar(@sig)) {
740                             if (defined($sign) && ++$sn==$sign) { last; } else { next; }
741                         }
742                     }
743
744                     if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
745                         if ($lsig && !$sig) {
746                             unshift(@sig, "[---=| Overlong signature removed by $0: " .
747                                 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
748                         }
749                         splice(@$lines, $x);
750                     }
751                     else {
752                         unshift(@sig, @$lines[$x..$#$lines]);
753                         splice(@$lines, $x);
754                     }
755                     if (defined($sign) && ++$sn==$sign) { last; } else { next; }
756                 }
757                 # any trailing newlines?
758                 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
759                 elsif ($chk_empty) { $chk_empty = 0; }
760             }
761         }
762     }
763
764     # Fix quote markers to adhere to RFC 3676, this changes "> >" to ">> " (if
765     # the default $indent is used). The space after ">" is not mandatory by RFC
766     # but makes the result more readable.
767     if ($fixind) {
768         for ($x=0; $x<scalar(@$lines); $x++) {
769             if (!$vrb[$x] && index($$lines[$x], $indent)==0) {
770                 # We match space at the beginning to prevent removal of spaces
771                 # directly after the last quote mark.
772                 $$lines[$x] =~ /^((\Q$indent\E| )*\Q$indent\E ?)(.*)$/;
773                 my $tmp = $1;
774                 my $len = length $1;
775                 my $rest = $3;           # if anything follows after the quote
776                 $tmp =~ tr/ //d;
777                 $tmp .= ' ' if $rest;    # don't create trailing whitespace
778                 substr($$lines[$x], 0, $len, $tmp);
779             }
780         }
781     }
782
783     # See if there is some Kammquoting to fix:
784     if ($kamm) { decomb($lines, \@vrb); }
785
786     # Now care about TOFU.
787     # One common mispractice is M$ and Groupwise style TOFU:
788     if ($ms||$gw) {
789         # bloat this array if you want more internationalization:
790         my @tofu = ('Original Message',
791                     'Original-Nachricht',
792                     'Ursprüngliche Nachricht',
793                     'Ursprüngliche Nachricht',
794                     'Ursprungliche Nachricht',
795                     'Mensagem original',
796                     'Ursprungligt meddelande',
797                     'Oorspronkelijk bericht',
798                     'Message d\'origine',
799                     'Forwarded message',
800                     'Weitergeleitete Nachricht / Forwarded Message');
801         my $k = 0;    # any text above?
802         my $tmp = 0;  # flagged if inside PGP output
803
804         DONE: for ($x=0; $x<scalar(@$lines); $x++) { 
805             if (!$vrb[$x]) {
806                 foreach my $tmp (@tofu) {
807                     if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
808                         ($gw &&
809                             ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
810                              $$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/))) { 
811                         $x++;
812                         $trad = 0;
813                         $bigqn = 0;
814                         last DONE; 
815                     }
816                 }
817
818                 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
819                     ((!$mua) ||
820                      ($mua eq 'mutt' &&
821                          # this line seems not necessary since mutt-1.5.18:
822                          $$lines[$x] !~ /^\e[^\a]+\a$/o &&
823                          $$lines[$x] !~
824                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
825                          $$lines[$x] !~
826                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
827                     ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
828                 {
829                     if ($mua eq 'mutt' && (!$tmp) &&
830                         $$lines[$x] =~
831                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
832                         $tmp = 1; 
833                     } elsif ($mua eq 'mutt' && $tmp && 
834                         ($$lines[$x] =~
835                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
836                          $$lines[$x] =~
837                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
838                         $tmp = 0;
839                     } elsif (!$tmp) {
840                         $k = 1;
841                     }
842                 }
843             }
844         }
845
846         # try to avoid false positives and only delete m$ style tofu if
847         # there is text above
848         if ($k) {
849             if (!$ms_smart) { goto CLEAN; }
850
851             # first, see if there is pgp stuff inside the tofu:
852             my $p = 0;    # levels of pgp signed parts
853
854             for (my $i=$x+1; $i<scalar(@$lines); $i++) {
855                 if ($$lines[$i] =~
856                         /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
857                     $p++;
858                 }
859             }
860             if ($p) {
861                 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
862                     if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
863                         splice(@att, 0, $i);
864                         unshift(@att, "\n");
865                         goto CLEAN;
866                     } elsif ($att[$i] =~
867                             /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
868                         splice(@att, 0, $i+1);
869                         $p--;
870                         goto STAIRS;
871                     }
872                 }
873                 splice(@att);
874             }
875
876             # now removing is safe:
877             CLEAN: $j = scalar(@$lines)-$x;
878             splice(@$lines, $x);
879         }
880     }
881
882     # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
883     # much more aggressively, so we won't need to search any more if we
884     # did find some):
885     if ($trad && (!$j) && !$vrb[$#$lines]) {
886         if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) { 
887             unshift(@sig, pop(@$lines));
888         }
889
890         my $k;
891         my $x = 1;
892
893         for (my $i=$#$lines; $i>=0; $i--) {
894             if ($vrb[$i]) { last; }
895             if (index($$lines[$i], $indent)==0) {
896                 $j++;
897                 $k = $i;
898             }
899             elsif ($$lines[$i] !~ /^\s*$/) { last; }
900         }
901
902         if ($j) {
903             # if there is no text above, we will assume the message is meant
904             # as forwarding and therefore OK
905             for (my $i=$k-1; $i>=0; $i--) {
906                 if ($$lines[$i] !~ /^\s*$/o) {
907                    $x = 0;
908                    last;
909                 }
910             }
911             if ($x) {
912                 $j = 0;
913             } else {
914                 splice(@$lines, $k);
915             }
916         }
917     }
918
919     # OK, if we found TOFU, we will leave a message that we were here...
920     if ($j) { 
921         # make sendmail bounce if we shall be picky 
922         # and indeed found something:
923         if ($mda) { 
924             if ($mda ne '1') { 
925                 print STDERR $boun;
926
927                 if ($sysl) {
928                     eval { require Sys::Syslog; }; 
929                     if ($@) { warn $@; } else {
930                         Sys::Syslog::setlogsock('unix');
931                         Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
932                         Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
933                         Sys::Syslog::closelog();
934                     }
935                 }
936             }
937
938             exit EX_BOUNCE;
939         }
940
941         # if we were invoked just for checking and indeed found something,
942         # print out the error message and quit:
943         if ($check) {
944             print $msg_quote;
945             exit EX_UNAVAILABLE;
946         }
947
948         push(@$lines, "[---=| TOFU protection by $0: " .
949             "$j lines snipped |=---]\n");
950     }
951     elsif ($mda eq '1') { exit EX_OK; }
952
953     # Care for huge blocks of quoted original message:
954     if ($bigqn) { debigq($lines, \@vrb); }
955
956     # Care for trailing whitespaces:
957     if ($trsp) {
958         for ($x=0; $x<scalar(@$lines); $x++) { 
959             if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
960         }
961     }
962
963     # Care for punctuation abuse:
964     if ($elli) {
965         for ($x=0; $x<scalar(@$lines); $x++) { 
966             if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
967         }
968     }
969
970     # (Nearly) at last care for multiple blank lines. (Do not do this
971     # earlier -- the way it is done right now would screw up the verbatim
972     # list)
973     if ($cr) {
974         # When handling regular TOFU above we move the last empty line from
975         # the body to the signature. This prevents the correct removal of
976         # empty lines before a signature (one line less is removed than it
977         # should) when -c and -t are used. This fixes it.
978         if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
979             push(@$lines, shift(@sig));
980         }
981
982         my $t = 0;
983         for ($x=scalar(@$lines)-1; $x>=0; $x--) {
984             if ((!$vrb[$x]) &&
985                 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
986                   $$lines[$x] =~ /^\s*$/o))
987             { 
988                 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
989             }
990             else { $t = 0; }
991         }
992     }
993
994     # Everything changing the body is done now. Time to fix the line count
995     # header so naive clients do not get confused. Just to be sure, append
996     # the old line count to X-headers.
997     my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
998                 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) + 
999                 (!$ad?scalar(@ads):0);
1000     if ($linecount-$l!=0) {
1001         for ($x=0; $x<scalar(@hdr); $x++) {
1002             if ($hdr[$x] =~ 
1003                 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1004             { 
1005                 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1006                 push(@hdr, "\n");
1007             }
1008         }
1009     }
1010
1011     # Finally, before leaving we put everything back in right order.
1012     unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1013     push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1014         @bo2);
1015 }
1016
1017
1018 # environment
1019 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1020                  ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1021
1022 # command line switches
1023 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1024     $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1025     $sani, $sig, $sigint, $spass, $trad, $trsp) =
1026     (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);
1027 my $ifile   = '-';    # use STDIN if nothing specified
1028
1029 # temp vals:
1030 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1031     $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1032
1033 # get command line params:
1034 if (!Getopt::Long::GetOptions(
1035     'a'             => \$ad,
1036     'A=s'           => \$ads,
1037     'bigq:s'        => \$_t_bigq,
1038     'body'          => \$_t_nohdr,
1039     'c:i'           => \$_t_cr,
1040     'check:s'       => \$_t_check,
1041     'debug|d'       => \$sysl,
1042     'diff'          => \$diff,
1043     'e'             => \$elli,
1044     'fixind'        => \$fixind,
1045     'ftr-ad'        => \$ftr_ad,
1046     'ftr-ml'        => \$ftr_ml,
1047     'groupwise'     => \$gw,
1048     'help|h'        => \$_t_help,
1049     'i=s'           => \$ifile,
1050     'k'             => \$kamm,
1051     'kminl=i'       => \$_t_kminl,
1052     'kmaxl=i'       => \$_t_kmaxl,
1053     'kdiff=i'       => \$_t_kdiff,
1054     'L=s'           => \$footers,
1055     'l'             => \$ml,
1056     'lax-security'  => \$lax,
1057     'locale=s'      => \$locale,
1058     'max-lines:i'   => \$maxlines,
1059     'ms-smart'      => \$ms_smart,
1060     'mua|M=s'       => \$_t_mua,
1061     'm'             => \$ms,
1062     'o=s'           => \$ofile,
1063     'P=s'           => \$boun,
1064     'p:s'           => \$_t_redir,
1065     'pgp-short'     => \$pgpshort,
1066     'pgp-move'      => \$pgpmove,
1067     'pgp-move-vrf'  => \$pgpmovevrf,
1068     'r'             => \$hdrs,
1069     'reply'         => \$reply,
1070     'S:i'           => \$_t_maxsig,
1071     's'             => \$sig,
1072     'sani'          => \$sani,
1073     'sigsmax:i'     => \$_t_sigsmax,
1074     'spass'         => \$spass,
1075     't'             => \$trad,
1076     'version|v'     => \$_t_ver,
1077     'w'             => \$trsp
1078     )) {
1079     help();
1080 }
1081
1082 # clean up temp vals:
1083 if (defined $_t_bigq) {
1084     if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1085     $bigqn = $1?$1:30; 
1086     $bigqx = $2?$2:10; 
1087     if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1088 }
1089 if (defined $_t_help) { help(); }
1090 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1091 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1092 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1093 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1094 if (defined $_t_mua) {
1095     $mua = lc($_t_mua);
1096     # mutt still displays the message when ^C'ing pgp verification:
1097     if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1098 }
1099 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1100 if (defined $_t_check) {
1101     $check = 1;
1102     while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1103         my $foo = $1;
1104         $_t_check = $2;
1105         if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1106     }
1107 }
1108 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1109 if (defined $_t_ver) { version(); }
1110 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1111 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1112 if ($ms_smart) { $ms = 1; }
1113
1114 if ($mua eq 'mutt') {
1115     if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1116         eval { require Locale::gettext; };
1117         if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1118             Locale::gettext::textdomain('mutt');
1119             ($mutt_attachment) =
1120                 Locale::gettext::gettext("[-- Attachment #%d") =~
1121                 m/^([^%]*)/;
1122             ($mutt_contenttype) =
1123                 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1124                 m/^([^%]*)/;
1125             ($mutt_pgpsigned) =
1126                 Locale::gettext::gettext("[-- End of signed data --]\n")  =~
1127                 m/^(.*)\n/m;
1128             ($mutt_beginsigned) =
1129                 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1130                 m/^(.*)\n/m;
1131             ($mutt_pgpclearsigned) =
1132                 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1133                 m/^(.*)\n/m;
1134             ($mutt_pgpclearsigstart) =
1135                 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1136                 m/^(.*)\n/m;
1137             ($mutt_pgpencrypted) =
1138                 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1139                 m/^(.*)\n/m;
1140             ($mutt_pgpoutstart) =
1141                 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1142                 m/^([^%]*)/;
1143             ($mutt_pgpoutend) =
1144                 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1145                 m/^(.*)\n/m;
1146
1147             Locale::gettext::textdomain('gnupg');
1148             ($gpg_WARNING) =
1149                 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1150                 m/^([^:]*: )/;
1151             ($gpg_Warning) =
1152                 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1153                 m/^([^:]*: )/;
1154             ($gpg_Cantcheck) =
1155                 Locale::gettext::gettext("Can't check signature: %s\n") =~
1156                 m/^([^%]*)/;
1157             ($gpg_aka) =
1158                 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1159                 m/^([^"«%]*)["«%]/;
1160             ($gpg_bad) =
1161                 Locale::gettext::gettext('BAD signature from "%s"') =~
1162                 m/^([^"«%]*)["«%]/;
1163             ($gpg_expired) =
1164                 Locale::gettext::gettext("Note: This key has expired!\n") =~
1165                 m/^(.*)\n/m;
1166             ($gpg_good) =
1167                 Locale::gettext::gettext('Good signature from "%s"') =~
1168                 m/^(([^"«%]*))["«%]/;
1169             ($gpg_bug) =
1170                 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1171                 m/^([^%]*)/;
1172         }
1173     }
1174
1175 }
1176 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1177  
1178 if (($ml && $footers eq '')||    # no -l without -L
1179     ($ad && $ads eq '')||        # no -a without -A
1180     ($nohdr && $pgpshort)||      # --body and --pgp-short are like oil and water
1181     (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1182     ($ifile eq '')||             # no empty -i
1183     ($ofile eq ''))              # no empty -o
1184 { help(); }
1185
1186
1187 # Read message:
1188 if (!open(IN, $ifile))
1189     { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1190 my @message = <IN>;
1191 close IN;
1192
1193 # First, check msg length and stop processing if msg is too long:
1194 if ((defined $maxlines) && (@message > $maxlines)) {
1195     if ($mua eq 'mutt') {
1196         my $x = 0;
1197         if (!$nohdr) {
1198             do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1199             $x++;
1200         }
1201         splice(@message, $x, 0,
1202             ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1203     }
1204     write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1205
1206     if ($mua eq 'mutt') { exit(EX_OK); }
1207     exit(EX_DATAERR);
1208 }
1209
1210 # this should be self-explanatory:
1211 process_msg(\@message);
1212
1213 # Finally, print clean lines:
1214 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1215
1216 # vim600:set foldmethod=marker:
1217 # eof