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