]> git.deb.at Git - pkg/t-prot.git/blob - t-prot
Merge commit 'upstream/2.100'
[pkg/t-prot.git] / t-prot
1 #!/usr/bin/perl -w
2 # $Id: t-prot,v 1.310 2010/07/10 19:14:36 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.100';
9 use constant REV            => '';
10 use constant REL            => q$Revision: 1.310 $=~m/(\d+(?:\.\d+)+)/;
11 # MTA expecting mail on STDIN
12 # (you might have to adjust this if using a different MTA)
13 use constant SENDMAIL       => '/usr/sbin/sendmail -oi';
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                     '[A-Za-z ]+ <(\w[\w.+-]*@(?:[\w.+-]+\.)+[A-Za-z]{2,})> schrieb:');
802         my $k = 0;    # any text above?
803         my $tmp = 0;  # flagged if inside PGP output
804
805         DONE: for ($x=0; $x<scalar(@$lines); $x++) { 
806             if (!$vrb[$x]) {
807                 foreach my $tmp (@tofu) {
808                     if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
809                         ($gw &&
810                             ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
811                              $$lines[$x] =~ /^>>> On [A-Z][a-z][a-z]?, [A-Z][a-z][a-z]? \d\d?, \d\d\d\d at [ \d]\d:\d\d [AP]M, in message/))) { 
812                         $x++;
813                         $trad = 0;
814                         $bigqn = 0;
815                         last DONE; 
816                     }
817                 }
818
819                 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
820                     ((!$mua) ||
821                      ($mua eq 'mutt' &&
822                          # this line seems not necessary since mutt-1.5.18:
823                          $$lines[$x] !~ /^\e[^\a]+\a$/o &&
824                          $$lines[$x] !~
825                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
826                          $$lines[$x] !~
827                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
828                     ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
829                 {
830                     if ($mua eq 'mutt' && (!$tmp) &&
831                         $$lines[$x] =~
832                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
833                         $tmp = 1; 
834                     } elsif ($mua eq 'mutt' && $tmp && 
835                         ($$lines[$x] =~
836                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
837                          $$lines[$x] =~
838                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
839                         $tmp = 0;
840                     } elsif (!$tmp) {
841                         $k = 1;
842                     }
843                 }
844             }
845         }
846
847         # try to avoid false positives and only delete m$ style tofu if
848         # there is text above
849         if ($k) {
850             if (!$ms_smart) { goto CLEAN; }
851
852             # first, see if there is pgp stuff inside the tofu:
853             my $p = 0;    # levels of pgp signed parts
854
855             for (my $i=$x+1; $i<scalar(@$lines); $i++) {
856                 if ($$lines[$i] =~
857                         /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
858                     $p++;
859                 }
860             }
861             if ($p) {
862                 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
863                     if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
864                         splice(@att, 0, $i);
865                         unshift(@att, "\n");
866                         goto CLEAN;
867                     } elsif ($att[$i] =~
868                             /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
869                         splice(@att, 0, $i+1);
870                         $p--;
871                         goto STAIRS;
872                     }
873                 }
874                 splice(@att);
875             }
876
877             # now removing is safe:
878             CLEAN: $j = scalar(@$lines)-$x;
879             splice(@$lines, $x);
880         }
881     }
882
883     # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
884     # much more aggressively, so we won't need to search any more if we
885     # did find some):
886     if ($trad && (!$j) && !$vrb[$#$lines]) {
887         if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) { 
888             unshift(@sig, pop(@$lines));
889         }
890
891         my $k;
892         my $x = 1;
893
894         for (my $i=$#$lines; $i>=0; $i--) {
895             if ($vrb[$i]) { last; }
896             if (index($$lines[$i], $indent)==0) {
897                 $j++;
898                 $k = $i;
899             }
900             elsif ($$lines[$i] !~ /^\s*$/) { last; }
901         }
902
903         if ($j) {
904             # if there is no text above, we will assume the message is meant
905             # as forwarding and therefore OK
906             for (my $i=$k-1; $i>=0; $i--) {
907                 if ($$lines[$i] !~ /^\s*$/o) {
908                    $x = 0;
909                    last;
910                 }
911             }
912             if ($x) {
913                 $j = 0;
914             } else {
915                 splice(@$lines, $k);
916             }
917         }
918     }
919
920     # OK, if we found TOFU, we will leave a message that we were here...
921     if ($j) { 
922         # make sendmail bounce if we shall be picky 
923         # and indeed found something:
924         if ($mda) { 
925             if ($mda ne '1') { 
926                 print STDERR $boun;
927
928                 if ($sysl) {
929                     eval { require Sys::Syslog; }; 
930                     if ($@) { warn $@; } else {
931                         Sys::Syslog::setlogsock('unix');
932                         Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
933                         Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
934                         Sys::Syslog::closelog();
935                     }
936                 }
937             }
938
939             exit EX_BOUNCE;
940         }
941
942         # if we were invoked just for checking and indeed found something,
943         # print out the error message and quit:
944         if ($check) {
945             print $msg_quote;
946             exit EX_UNAVAILABLE;
947         }
948
949         push(@$lines, "[---=| TOFU protection by $0: " .
950             "$j lines snipped |=---]\n");
951     }
952     elsif ($mda eq '1') { exit EX_OK; }
953
954     # Care for huge blocks of quoted original message:
955     if ($bigqn) { debigq($lines, \@vrb); }
956
957     # Care for trailing whitespaces:
958     if ($trsp) {
959         for ($x=0; $x<scalar(@$lines); $x++) { 
960             if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
961         }
962     }
963
964     # Care for punctuation abuse:
965     if ($elli) {
966         for ($x=0; $x<scalar(@$lines); $x++) { 
967             if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
968         }
969     }
970
971     # (Nearly) at last care for multiple blank lines. (Do not do this
972     # earlier -- the way it is done right now would screw up the verbatim
973     # list)
974     if ($cr) {
975         # When handling regular TOFU above we move the last empty line from
976         # the body to the signature. This prevents the correct removal of
977         # empty lines before a signature (one line less is removed than it
978         # should) when -c and -t are used. This fixes it.
979         if (scalar(@sig) && $sig[0] =~ /^\s*$/) {
980             push(@$lines, shift(@sig));
981         }
982
983         my $t = 0;
984         for ($x=scalar(@$lines)-1; $x>=0; $x--) {
985             if ((!$vrb[$x]) &&
986                 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
987                   $$lines[$x] =~ /^\s*$/o))
988             { 
989                 if ($t<$cr) { $t++; } else { splice(@$lines, $x, 1); }
990             }
991             else { $t = 0; }
992         }
993     }
994
995     # Everything changing the body is done now. Time to fix the line count
996     # header so naive clients do not get confused. Just to be sure, append
997     # the old line count to X-headers.
998     my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
999                 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) + 
1000                 (!$ad?scalar(@ads):0);
1001     if ($linecount-$l!=0) {
1002         for ($x=0; $x<scalar(@hdr); $x++) {
1003             if ($hdr[$x] =~ 
1004                 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
1005             { 
1006                 $hdr[$#hdr] = "X-Old-Lines: $2\n";
1007                 push(@hdr, "\n");
1008             }
1009         }
1010     }
1011
1012     # Finally, before leaving we put everything back in right order.
1013     unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
1014     push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
1015         @bo2);
1016 }
1017
1018
1019 # environment
1020 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
1021                  ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
1022
1023 # command line switches
1024 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $fixind, $footers, $lax,
1025     $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
1026     $sani, $sig, $sigint, $spass, $trad, $trsp) =
1027     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1028 my $ifile   = '-';    # use STDIN if nothing specified
1029
1030 # temp vals:
1031 my ($_t_bigq, $_t_help, $_t_kminl, $_t_kmaxl, $_t_kdiff, $_t_maxsig, $_t_mua,
1032     $_t_redir, $_t_check, $_t_sigsmax, $_t_ver, $_t_nohdr, $_t_cr) = undef;
1033
1034 # get command line params:
1035 if (!Getopt::Long::GetOptions(
1036     'a'             => \$ad,
1037     'A=s'           => \$ads,
1038     'bigq:s'        => \$_t_bigq,
1039     'body'          => \$_t_nohdr,
1040     'c:i'           => \$_t_cr,
1041     'check:s'       => \$_t_check,
1042     'debug|d'       => \$sysl,
1043     'diff'          => \$diff,
1044     'e'             => \$elli,
1045     'fixind'        => \$fixind,
1046     'ftr-ad'        => \$ftr_ad,
1047     'ftr-ml'        => \$ftr_ml,
1048     'groupwise'     => \$gw,
1049     'help|h'        => \$_t_help,
1050     'i=s'           => \$ifile,
1051     'k'             => \$kamm,
1052     'kminl=i'       => \$_t_kminl,
1053     'kmaxl=i'       => \$_t_kmaxl,
1054     'kdiff=i'       => \$_t_kdiff,
1055     'L=s'           => \$footers,
1056     'l'             => \$ml,
1057     'lax-security'  => \$lax,
1058     'locale=s'      => \$locale,
1059     'max-lines:i'   => \$maxlines,
1060     'ms-smart'      => \$ms_smart,
1061     'mua|M=s'       => \$_t_mua,
1062     'm'             => \$ms,
1063     'o=s'           => \$ofile,
1064     'P=s'           => \$boun,
1065     'p:s'           => \$_t_redir,
1066     'pgp-short'     => \$pgpshort,
1067     'pgp-move'      => \$pgpmove,
1068     'pgp-move-vrf'  => \$pgpmovevrf,
1069     'r'             => \$hdrs,
1070     'reply'         => \$reply,
1071     'S:i'           => \$_t_maxsig,
1072     's'             => \$sig,
1073     'sani'          => \$sani,
1074     'sigsmax:i'     => \$_t_sigsmax,
1075     'spass'         => \$spass,
1076     't'             => \$trad,
1077     'version|v'     => \$_t_ver,
1078     'w'             => \$trsp
1079     )) {
1080     help();
1081 }
1082
1083 # clean up temp vals:
1084 if (defined $_t_bigq) {
1085     if ($_t_bigq !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1086     $bigqn = $1?$1:30; 
1087     $bigqx = $2?$2:10; 
1088     if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1089 }
1090 if (defined $_t_help) { help(); }
1091 if (defined $_t_kminl) { $kminl = $_t_kminl; $kamm = 1; }
1092 if (defined $_t_kmaxl) { $kmaxl = $_t_kmaxl; $kamm = 1; }
1093 if (defined $_t_kdiff) { $kdiff = $_t_kdiff; $kamm = 1; }
1094 if (defined $_t_maxsig) { $lsig = $_t_maxsig ? $_t_maxsig : $maxsig; }
1095 if (defined $_t_mua) {
1096     $mua = lc($_t_mua);
1097     # mutt still displays the message when ^C'ing pgp verification:
1098     if ($mua eq 'mutt') { $SIG{'INT'} = 'sigint_handler'; }
1099 }
1100 if (defined $_t_redir) { $mda = $_t_redir ? $_t_redir : '1'; }
1101 if (defined $_t_check) {
1102     $check = 1;
1103     while ($_t_check && $_t_check =~ /^([^,\s]+)(?:,(\S+))?$/) {
1104         my $foo = $1;
1105         $_t_check = $2;
1106         if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) { $check_ratio = $1?$1:0.75; }
1107     }
1108 }
1109 if (defined $_t_sigsmax) { $sign = $_t_sigsmax ? $_t_sigsmax : undef; }
1110 if (defined $_t_ver) { version(); }
1111 if (defined $_t_nohdr) { $nohdr=1; $hdrs=1; }
1112 if (defined $_t_cr) { $cr = $_t_cr ? $_t_cr : $crshrink; }
1113 if ($ms_smart) { $ms = 1; }
1114
1115 if ($mua eq 'mutt') {
1116     if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1117         eval { require Locale::gettext; };
1118         if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1119             Locale::gettext::textdomain('mutt');
1120             ($mutt_attachment) =
1121                 Locale::gettext::gettext("[-- Attachment #%d") =~
1122                 m/^([^%]*)/;
1123             ($mutt_contenttype) =
1124                 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1125                 m/^([^%]*)/;
1126             ($mutt_pgpsigned) =
1127                 Locale::gettext::gettext("[-- End of signed data --]\n")  =~
1128                 m/^(.*)\n/m;
1129             ($mutt_beginsigned) =
1130                 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1131                 m/^(.*)\n/m;
1132             ($mutt_pgpclearsigned) =
1133                 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1134                 m/^(.*)\n/m;
1135             ($mutt_pgpclearsigstart) =
1136                 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1137                 m/^(.*)\n/m;
1138             ($mutt_pgpencrypted) =
1139                 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1140                 m/^(.*)\n/m;
1141             ($mutt_pgpoutstart) =
1142                 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1143                 m/^([^%]*)/;
1144             ($mutt_pgpoutend) =
1145                 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1146                 m/^(.*)\n/m;
1147
1148             Locale::gettext::textdomain('gnupg');
1149             ($gpg_WARNING) =
1150                 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1151                 m/^([^:]*: )/;
1152             ($gpg_Warning) =
1153                 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1154                 m/^([^:]*: )/;
1155             ($gpg_Cantcheck) =
1156                 Locale::gettext::gettext("Can't check signature: %s\n") =~
1157                 m/^([^%]*)/;
1158             ($gpg_aka) =
1159                 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1160                 m/^([^"«%]*)["«%]/;
1161             ($gpg_bad) =
1162                 Locale::gettext::gettext('BAD signature from "%s"') =~
1163                 m/^([^"«%]*)["«%]/;
1164             ($gpg_expired) =
1165                 Locale::gettext::gettext("Note: This key has expired!\n") =~
1166                 m/^(.*)\n/m;
1167             ($gpg_good) =
1168                 Locale::gettext::gettext('Good signature from "%s"') =~
1169                 m/^(([^"«%]*))["«%]/;
1170             ($gpg_bug) =
1171                 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1172                 m/^([^%]*)/;
1173         }
1174     }
1175
1176 }
1177 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1178  
1179 if (($ml && $footers eq '')||    # no -l without -L
1180     ($ad && $ads eq '')||        # no -a without -A
1181     ($nohdr && $pgpshort)||      # --body and --pgp-short are like oil and water
1182     (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitizing without headers :)
1183     ($ifile eq '')||             # no empty -i
1184     ($ofile eq ''))              # no empty -o
1185 { help(); }
1186
1187
1188 # Read message:
1189 if (!open(IN, $ifile))
1190     { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1191 my @message = <IN>;
1192 close IN;
1193
1194 # First, check msg length and stop processing if msg is too long:
1195 if ((defined $maxlines) && (@message > $maxlines)) {
1196     if ($mua eq 'mutt') {
1197         my $x = 0;
1198         if (!$nohdr) {
1199             do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1200             $x++;
1201         }
1202         splice(@message, $x, 0,
1203             ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1204     }
1205     write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1206
1207     if ($mua eq 'mutt') { exit(EX_OK); }
1208     exit(EX_DATAERR);
1209 }
1210
1211 # this should be self-explanatory:
1212 process_msg(\@message);
1213
1214 # Finally, print clean lines:
1215 write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1216
1217 # vim600:set foldmethod=marker:
1218 # eof