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