]> git.deb.at Git - pkg/t-prot.git/blob - t-prot
Imported Upstream version 2.15
[pkg/t-prot.git] / t-prot
1 #!/usr/bin/perl -w
2 # $Id: t-prot,v 1.285 2010/02/15 20:13:00 jochen Exp $
3
4 require 5.006;
5 use strict;
6 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
7 use Getopt::Mixed qw(nextOption);
8 use constant VER            => '2.15';
9 use constant REV            => '';
10 use constant REL            => q$Revision: 1.285 $=~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 $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 $indent         = '>';    # Indent string, regexp to identify a quoted line
46 $kminl          = 65;     # see decomb() for details
47 $kmaxl          = 80;
48 $kdiff          = 20;
49 $pgpshort       = 0;      # hide pgp key ids if set
50 $pgpmove        = 0;      # move pgp output to bottom if set
51 $pgpmovevrf     = 0;
52 $sign           = 1;      # max number of sigs tolerated, undef for no limit
53 $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";
54 $ftr_ad         = undef;  # too hard on performance to be default
55 $ftr_ml         = undef;  # too hard on performance to be default
56 $ofile          = '-';    # use STDOUT if nothing is specified
57 $spass_prefix   = 'SPAM: ';
58 $check_ratio    = 0;      # off by default
59 $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";
60 $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";
61 # end of user adjusted vals
62
63
64 # set the defaults to the C locale
65 $mutt_attachment        = '[-- Attachment #';
66 $mutt_contenttype       = '[-- Type: ';
67 $mutt_pgpsigned         = '[-- End of signed data --]';
68 $mutt_beginsigned       = '[-- The following data is signed --]';
69 $mutt_pgpclearsigstart  = '[-- BEGIN PGP SIGNED MESSAGE --]';
70 $mutt_pgpclearsigned    = '[-- END PGP SIGNED MESSAGE --]';
71 $mutt_pgpencrypted      = '[-- End of PGP/MIME encrypted data --]';
72 $mutt_pgpoutstart       = '[-- PGP output follows (current time:';
73 $mutt_pgpoutend         = '[-- End of PGP output --]';
74
75 # set the defaults to the C locale
76 $gpg_WARNING            = 'WARNING: ';
77 # (yes, the translations in gnupg's po files *are* braindamaged):
78 $gpg_Warning            = 'WARNING: ';
79 $gpg_Cantcheck          = 'Can\'t check signature: ';
80 $gpg_aka                = '                aka ';
81 $gpg_bad                = 'BAD signature from ';
82 $gpg_expired            = 'Note: This key has expired!';
83 $gpg_good               = 'Good signature from';
84 # (actually, this bugs me quite often since upgrading gpg from v1.0.7):
85 $gpg_bug                = '... this is a bug (';
86
87
88 # help(): print help text and exit with appropriate exit code
89 sub help {
90     print "Usage: $0 [options] 
91   -A=DIRECTORY    ad footer directory, treat ad footers as signature
92   -a              remove ad footers; requires -A
93   --bigq[=n[,x]]  remove all but x lines of quotes with more than n
94                   lines; default is n=30 and x=10
95   --body          input has no headers; does not work with --pgp-short;
96                   multipart messages will not be detected
97   -c              merge multiple blank lines
98   --check[=FLAGS] check various criteria, print error message and quit;
99                   see man page for details
100   -d, --debug     print notice to syslog when bouncing; requires -p
101   --diff          tolerate diffs appended *after* the signature
102   -e              force ellipsis for excessive punctuation
103   --ftr-ad        enable aggressive ad footer matching; requires -A
104   --ftr-ml        enable aggressive mailing list footer matching; req. -L
105   --groupwise     delete Novell Groupwise style TOFU
106   -h, --help      show this short help and exit
107   -i=INFILE       file to be read; '-' for STDIN (default)
108   -k              try to fix \"Kammquotes\"
109   --kminl=n       min. line length for wrapped line; requires -k
110   --kmaxl=n       max. line length for wrapped line; requires -k
111   --kdiff=n       max. length difference between wrapped lines; req. -k
112   -L=DIRECTORY    mailing list footer directory, treat mailing list
113                   footers as signature
114   -l              delete mailing list footer; requires -L
115   --lax-security  use unsafe writing method; USE ON YOUR OWN RISK!
116   --locale=LOCALE internationalization; currently only used with -Mmutt
117   -M, --mua=MUA   turn on special treatment for some mail user agents
118   -m              delete MS style TOFU; careful: might be too agressive
119   --max-lines=x   maximum number of message lines
120   --ms-smart      try to be smart with MS style TOFU; req. -Mmutt and -m
121   -o=OUTFILE      file to be written to; '-' for STDOUT (default), 'NONE'
122                   for no output at all
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 shrinked 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 '-')) {
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         } else { $vrb[$x] = $verb; }
608     }
609
610     # Calculate quoting ratio (with respect to verbatims):
611     if ($check && scalar(@$lines)) {
612         my ($y, $z) = (0, 0);
613         for ($x=0; $x<scalar(@$lines); $x++) {
614             if (!$vrb[$x]) {
615                 $z++;
616                 if (index($$lines[$x], $indent)==0) { $y++; }
617             }
618         }
619
620         if ($y/$z>=$check_ratio) {
621             print $msg_ratio;
622             exit EX_UNAVAILABLE;
623         }
624     }
625
626     if ($mua eq 'mutt') {
627         # See if we find pgp output generated by mutt before we scramble
628         # the thing. If yes, see if we can beautify it.
629         if ($pgpshort || $pgpmove || $pgpmovevrf) { pgp($lines, \@vrb, \@hdr); }
630
631         # Remove all but the first attachment (if this is text/plain)
632         # mutt did introduce (bah!). Remember, all this ugliness could
633         # be replaced with a proper and clean edit_filter patch in 
634         # mutt(1) itself...
635         for ($x=$#$lines; $x>=0; $x--) {
636             if ($vrb[$x]) { next; }
637             # The following regexp's are quite ugly because for most users
638             # these lines are coloured using termcap... (bah!)
639             if (($$lines[$x] =~
640                      /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E(\d+)/o &&
641                     (($1 ne '1') ||
642                     ($x<$#$lines &&
643                         $$lines[$x+1] !~ m!^
644                             (?:\e[^\a]+\a)?
645                             (?:\Q$mutt_contenttype\E)
646                             (?:text/plain|application/pgp)
647                         !ox))) ||
648                 ($$lines[$x] =~ /^
649                     (?:\e[^\a]+\a)?
650                     (?:\Q$mutt_pgpsigned\E     |
651                        \Q$mutt_pgpclearsigned\E|
652                        \Q$mutt_pgpencrypted\E)
653                 /ox))
654             { 
655                 # Strip attachments to prepare further processing
656                 unshift(@att, @$lines[$x..$#$lines]);
657                 splice(@$lines, $x);
658                 # Try to fix trailing empty lines
659                 while (scalar(@$lines) && $$lines[$#$lines] =~
660                     /^(?:\e[^\a]+\a)?\s*$/) { 
661                     unshift(@att, pop(@$lines));
662                 }
663
664                 # Remove ML and ad footers within attachments:
665                 my @tmp;
666                 if ($ml) { remove_footers($lines, \@tmp, $footers, undef); }
667                 if ($ad) { remove_footers($lines, \@tmp, $ads,     undef); }
668                 $x = scalar(@$lines);
669             }
670         }
671
672         # care about the rest
673         if (scalar(@att)) {
674             for ($x=0; $x<$#att; $x++) {
675                 if ($vrb[scalar(@$lines)+$x]) { next; }
676
677                 # Pipe message/rfc822 parts to another instance of
678                 # process_msg() for further processing.
679                 # Please note that we cannot see what a hierarchy the
680                 # original message had -- if there were message/rfc822
681                 # parts within other message/rfc822 parts constellations
682                 # can occur which we cannot resolve. Therefore we simply
683                 # do not even try to be smart. This should work for most
684                 # situations.
685                 # The following regexp is quite ugly because for most
686                 # users the line is coloured using termcap... (bah!)
687                 if ($att[$x] =~
688                         /^(?:\e[^\a]+\a)?\Q$mutt_attachment\E\d+/o &&
689                     $att[$x+1] =~ m!^
690                         (?:\e[^\a]+\a)?
691                         (?:\Q$mutt_contenttype\E) (?:message/rfc822|multipart/alternative)
692                     !ox)
693                 {
694                     $x += 2;
695                     while ($att[$x] !~ /^\s*$/) { $x++; }
696                     $x++;
697
698                     my @tmp = @att[$x..$#att];
699                     process_msg(\@tmp);
700                     splice(@att, $x, scalar(@att)-$x, @tmp);
701                     $x += scalar(@tmp);
702                 }
703             }
704         }
705     }
706
707     # Remove signature:
708     if (scalar(@$lines)) { 
709         my $sn = 0;
710         my $chk_empty = 1;
711         my $empty = 0;
712
713         for ($x = $#$lines; $x>=0; $x--) {
714             if (!$vrb[$x]) {
715                 if ($$lines[$x] =~ /^-- $/) {
716                     if ($diff) {
717                         for (my $i=1; $x+$i+1<scalar(@$lines); $i++) {
718                             if ($$lines[$x+$i] =~ /^-{3}\s+\S/ &&
719                                 $$lines[$x+$i+1] =~ /^\+{3}\s+\S/)
720                             {
721                                 $sig = 0;
722                                 unshift(@sig, @$lines[$x..$#$lines]);
723                                 splice(@$lines, $x);
724                                 last;
725                             }
726                         }
727                         if (scalar(@sig)) {
728                             if (defined($sign) && ++$sn==$sign) { last; } else { next; }
729                         }
730                     }
731
732                     if ($sig || ($lsig && ($#$lines-$x-$empty>$lsig))) {
733                         if ($lsig && !$sig) {
734                             unshift(@sig, "[---=| Overlong signature removed by $0: " .
735                                 (scalar(@$lines)-$x) . " lines snipped |=---]\n");
736                         }
737                         splice(@$lines, $x);
738                     }
739                     else {
740                         unshift(@sig, @$lines[$x..$#$lines]);
741                         splice(@$lines, $x);
742                     }
743                     if (defined($sign) && ++$sn==$sign) { last; } else { next; }
744                 }
745                 # any trailing newlines?
746                 elsif ($chk_empty && $$lines[$x] =~ /^\s*$/) { $empty++; }
747                 elsif ($chk_empty) { $chk_empty = 0; }
748             }
749         }
750     }
751
752     # See if there is some Kammquoting to fix:
753     if ($kamm) { decomb($lines, \@vrb); }
754
755     # Now care about TOFU.
756     # One common mispractice is M$ and Groupwise style TOFU:
757     if ($ms||$gw) {
758         # bloat this array if you want more internationalization:
759         my @tofu = ('Original Message',
760                     'Original-Nachricht',
761                     'Ursprüngliche Nachricht',
762                     'Ursprüngliche Nachricht',
763                     'Ursprungliche Nachricht',
764                     'Mensagem original',
765                     'Ursprungligt meddelande',
766                     'Oorspronkelijk bericht',
767                     'Message d\'origine',
768                     'Forwarded message',
769                     'Weitergeleitete Nachricht / Forwarded Message');
770         my $k = 0;    # any text above?
771         my $tmp = 0;  # flagged if inside PGP output
772
773         DONE: for ($x=0; $x<scalar(@$lines); $x++) { 
774             if (!$vrb[$x]) {
775                 foreach my $tmp (@tofu) {
776                     if (($ms && $$lines[$x] =~ /^\s?-+\s?$tmp\s?-+/) ||
777                         ($gw &&
778                             ($$lines[$x] =~ /^>>>[^\<]+<[^\>]+> \d\d?\/\d\d?\/\d\d? \d\d?:\d\d [AP]M >>>/ ||
779                              $$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/))) { 
780                         $x++;
781                         $trad = 0;
782                         $bigqn = 0;
783                         last DONE; 
784                     }
785                 }
786
787                 if ((!$k) && $$lines[$x] !~ /^\s*$/o &&
788                     ((!$mua) ||
789                      ($mua eq 'mutt' &&
790                          # this line seems not necessary since mutt-1.5.18:
791                          $$lines[$x] !~ /^\e[^\a]+\a$/o &&
792                          $$lines[$x] !~
793                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_attachment\E)/o &&
794                          $$lines[$x] !~
795                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_contenttype\E)/o)) &&
796                     ((!$spass) || index($$lines[$x], $spass_prefix)!=0))
797                 {
798                     if ($mua eq 'mutt' && (!$tmp) &&
799                         $$lines[$x] =~
800                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpoutstart\E)/o) {
801                         $tmp = 1; 
802                     } elsif ($mua eq 'mutt' && $tmp && 
803                         ($$lines[$x] =~
804                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_beginsigned\E)/o ||
805                          $$lines[$x] =~
806                              /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o)) {
807                         $tmp = 0;
808                     } elsif (!$tmp) {
809                         $k = 1;
810                     }
811                 }
812             }
813         }
814
815         # try to avoid false positives and only delete m$ style tofu if
816         # there is text above
817         if ($k) {
818             if (!$ms_smart) { goto CLEAN; }
819
820             # first, see if there is pgp stuff inside the tofu:
821             my $p = 0;    # levels of pgp signed parts
822
823             for (my $i=$x+1; $i<scalar(@$lines); $i++) {
824                 if ($$lines[$i] =~
825                         /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigstart\E)/o) {
826                     $p++;
827                 }
828             }
829             if ($p) {
830                 STAIRS: for (my $i=0; $i<scalar(@att); $i++) {
831                     if ($p==0 && $att[$i] =~ /^(?:\e[^\a]+\a)?\[-- /o) {
832                         splice(@att, 0, $i);
833                         unshift(@att, "\n");
834                         goto CLEAN;
835                     } elsif ($att[$i] =~
836                             /^(?:\e[^\a]+\a)?(?:\Q$mutt_pgpclearsigned\E)/o) {
837                         splice(@att, 0, $i+1);
838                         $p--;
839                         goto STAIRS;
840                     }
841                 }
842                 splice(@att);
843             }
844
845             # now removing is safe:
846             CLEAN: $j = scalar(@$lines)-$x;
847             splice(@$lines, $x);
848         }
849     }
850
851     # Nothing? Then try traditional TOFU (deleting M$ style TOFU is done
852     # much more aggressively, so we won't need to search any more if we
853     # did find some):
854     if ($trad && (!$j) && !$vrb[$#$lines]) {
855         if (scalar(@$lines) && $$lines[$#$lines] =~ /^\s*$/) { 
856             unshift(@sig, pop(@$lines));
857         }
858
859         my $k;
860         my $x = 1;
861
862         for (my $i=$#$lines; $i>=0; $i--) {
863             if ($$lines[$i] =~ /^$indent/o) {
864                 $j++;
865                 $k = $i;
866             }
867             elsif ($$lines[$i] !~ /^\s*$/) { last; }
868         }
869
870         if ($j) {
871             # if there is no text above, we will assume the message is meant
872             # as forwarding and therefore OK
873             for (my $i=$k-1; $i>=0; $i--) {
874                 if ($$lines[$i] !~ /^\s*$/o) {
875                    $x = 0;
876                    last;
877                 }
878             }
879             if ($x) {
880                 $j = 0;
881             } else {
882                 splice(@$lines, $k);
883             }
884         }
885     }
886
887     # OK, if we found TOFU, we will leave a message that we were here...
888     if ($j) { 
889         # make sendmail bounce if we shall be picky 
890         # and indeed found something:
891         if ($mda) { 
892             if ($mda ne '1') { 
893                 print STDERR $boun;
894
895                 if ($sysl) {
896                     eval { require Sys::Syslog; }; 
897                     if ($@) { warn $@; } else {
898                         Sys::Syslog::setlogsock('unix');
899                         Sys::Syslog::openlog("$0[$$]", 'pid', 'mail');
900                         Sys::Syslog::syslog('debug', 'bounced message %s', $hdr[0]);
901                         Sys::Syslog::closelog();
902                     }
903                 }
904             }
905
906             exit EX_BOUNCE;
907         }
908
909         # if we were invoked just for checking and indeed found something,
910         # print out the error message and quit:
911         if ($check) {
912             print $msg_quote;
913             exit EX_UNAVAILABLE;
914         }
915
916         push(@$lines, "[---=| TOFU protection by $0: " .
917             "$j lines snipped |=---]\n");
918     }
919     elsif ($mda eq '1') { exit EX_OK; }
920
921     # Care for huge blocks of quoted original message:
922     if ($bigqn) { debigq($lines, \@vrb); }
923
924     # Care for trailing whitespaces:
925     if ($trsp) {
926         for ($x=0; $x<scalar(@$lines); $x++) { 
927             if (!$vrb[$x]) { $$lines[$x] =~ s/[\ \t]+$//o; }
928         }
929     }
930
931     # Care for punctuation abuse:
932     if ($elli) {
933         for ($x=0; $x<scalar(@$lines); $x++) { 
934             if (!$vrb[$x]) { $$lines[$x] =~ s/(([.?!])\2\2)\2+/$1/go; }
935         }
936     }
937
938     # (Nearly) at last care for multiple blank lines. (Do not do this
939     # earlier -- the way it is done right now would screw up the verbatim
940     # list)
941     if ($cr) {
942         my $t = 0;
943         for ($x=scalar(@$lines)-1; $x>=0; $x--) {
944             if ((!$vrb[$x]) &&
945                 (($mua eq 'mutt' && $$lines[$x] =~ /^\e[^\a]+\a\s*$/o) ||
946                   $$lines[$x] =~ /^\s*$/o))
947             { 
948                 if ($t<2) { $t++; } else { splice(@$lines, $x, 1); }
949             }
950             else { $t = 0; }
951         }
952     }
953
954     # Everything changing the body is done now. Time to fix the line count
955     # header so naive clients do not get confused. Just to be sure, append
956     # the old line count to X-headers.
957     my $l = scalar(@bo1) + scalar(@$lines) + scalar(@att) + scalar(@bo2) +
958                 (!$sig?scalar(@sig):0) + (!$ml?scalar(@ftr):0) + 
959                 (!$ad?scalar(@ads):0);
960     if ($linecount-$l!=0) {
961         for ($x=0; $x<scalar(@hdr); $x++) {
962             if ($hdr[$x] =~ 
963                 s/^(Lines:\s+)(\d+)/$1.($2-$linecount+$l)/e)
964             { 
965                 $hdr[$#hdr] = "X-Old-Lines: $2\n";
966                 push(@hdr, "\n");
967             }
968         }
969     }
970
971     # Finally, before leaving we put everything back in right order.
972     unshift(@$lines, (!$hdrs?@hdr:()), @bo1);
973     push(@$lines, (!$sig?@sig:()), (!$ad?@ads:()), (!$ml?@ftr:()), @att,
974         @bo2);
975 }
976
977
978 # environment
979 my $locale = $ENV{'LC_ALL'}?$ENV{'LC_ALL'}:
980                  ($ENV{'LC_MESSAGES'}?$ENV{'LC_MESSAGES'}:$ENV{'LANG'});
981
982 # command line switches
983 ($ad, $ads, $bigqn, $bigqx, $check, $cr, $sysl, $diff, $elli, $footers, $lax,
984     $ml, $gw, $ms, $ms_smart, $mda, $mua, $hdrs, $kamm, $lsig, $nohdr, $reply,
985     $sani, $sig, $sigint, $spass, $trad, $trsp) =
986     (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);
987 my $ifile   = '-';    # use STDIN if nothing specified
988
989 # get command line params:
990 Getopt::Mixed::init('a A=s c d e h i=s k L=s l m M=s o=s P=s p:s r S:i'.
991     ' s t v w bigq:s body check:s debug>d diff ftr-ad ftr-ml groupwise'.
992     ' help>h kminl=i kmaxl=i kdiff=i lax-security locale=s max-lines=i'.
993     ' ms-smart mua>M pgp-short pgp-move pgp-move-vrf reply sani sigsmax:i'.
994     ' spass version>v');
995 while (my ($opt, $val, $pretty) = nextOption()) {
996     if    ($opt eq 'a')     { $ad = 1; }
997     elsif ($opt eq 'A')     { $ads = $val; }
998     elsif ($opt eq 'bigq')  { 
999         if ($val !~ /^(?:(\d+)(?:,(\d+))?)?$/) { help(); }
1000         $bigqn = $1?$1:30; 
1001         $bigqx = $2?$2:10;
1002         if ($bigqn<=0 || $bigqx<=0 || $bigqn<=$bigqx) { help(); }
1003     }
1004     elsif ($opt eq 'body')  { $nohdr = 1; $hdrs = 1; }
1005     elsif ($opt eq 'c')     { $cr = 1; }
1006     elsif ($opt eq 'check') {
1007         $check = 1;
1008         while ($val && $val =~ /^([^,\s]+)(?:,(\S+))?$/) {
1009             my $foo = $1;
1010             $val = $2;
1011
1012             if ($foo =~ /^ratio(?:=(0?\.\d+))?$/) {
1013                 $check_ratio = $1?$1:0.75;
1014             }
1015         }
1016     }
1017     elsif ($opt eq 'd')     { $sysl = 1; }
1018     elsif ($opt eq 'diff')  { $diff = 1; }
1019     elsif ($opt eq 'e')     { $elli = 1; }
1020     elsif ($opt eq 'ftr-ad') { $ftr_ad = 1; $ad = 1; }
1021     elsif ($opt eq 'ftr-ml') { $ftr_ml = 1; $ml = 1; }
1022     elsif ($opt eq 'groupwise') { $gw = 1; }
1023     elsif ($opt eq 'i')     { $ifile = $val; }
1024     elsif ($opt eq 'k')     { $kamm = 1; }
1025     elsif ($opt eq 'kminl') { $kminl = $val; $kamm = 1; }
1026     elsif ($opt eq 'kmaxl') { $kmaxl = $val; $kamm = 1; }
1027     elsif ($opt eq 'kdiff') { $kdiff = $val; $kamm = 1; }
1028     elsif ($opt eq 'L')     { $footers = $val; }
1029     elsif ($opt eq 'l')     { $ml = 1; }
1030     elsif ($opt eq 'lax-security') { $lax = 1; }
1031     elsif ($opt eq 'locale') { $locale = $val; }
1032     elsif ($opt eq 'm')     { $ms = 1; }
1033     elsif ($opt eq 'max-lines') { $maxlines = $val; }
1034     elsif ($opt eq 'ms-smart') { $ms_smart = 1; $ms = 1; }
1035     elsif ($opt eq 'M') {
1036         $mua = lc($val);
1037
1038         if ($mua eq 'mutt') {
1039             # mutt still displays the message when ^C'ing pgp verification:
1040             $SIG{'INT'} = 'sigint_handler';
1041         }
1042     }
1043     elsif ($opt eq 'o')     { $ofile = $val; }
1044     elsif ($opt eq 'P')     { $boun = $val; }
1045     elsif ($opt eq 'p')     { $mda = $val ? $val : '1'; }
1046     elsif ($opt eq 'pgp-short') { $pgpshort = 1; }
1047     elsif ($opt eq 'pgp-move') { $pgpmove = 1; }
1048     elsif ($opt eq 'pgp-move-vrf') { $pgpmovevrf = 1; }
1049     elsif ($opt eq 'r')     { $hdrs = 1; }
1050     elsif ($opt eq 'reply') { $reply = 1; }
1051     elsif ($opt eq 'S')     { $lsig = $val ? $val : $maxsig; }
1052     elsif ($opt eq 's')     { $sig = 1; }
1053     elsif ($opt eq 'sani')  { $sani = 1; }
1054     elsif ($opt eq 'sigsmax') { $sign = $val ? $val : undef; }
1055     elsif ($opt eq 'spass') { $spass = 1; }
1056     elsif ($opt eq 't')     { $trad = 1; }
1057     elsif ($opt eq 'v')     { version(); }
1058     elsif ($opt eq 'w')     { $trsp = 1; }
1059     else                    { help(); }
1060 }
1061 Getopt::Mixed::cleanup();
1062 if (($ml && $footers eq '')||     # no -l without -L
1063     ($ad && $ads eq '')||         # no -a without -A
1064     ($nohdr && $pgpshort)||       # --body and --pgp-short are oil and water
1065     (($nohdr||$hdrs) && ($sani||$reply))|| # no sanitazing without headers :)
1066     ($ifile eq '')||              # no empty -i
1067     ($ofile eq ''))               # no empty -o
1068 { help(); }
1069
1070
1071 if ($mua eq 'mutt') {
1072     if (defined $locale && $locale ne '' && $locale ne 'C' && $locale ne 'POSIX') {
1073         eval { require Locale::gettext; };
1074         if ($@) { warn $@; exit(EX_SOFTWARE); } else {
1075             Locale::gettext::textdomain('mutt');
1076             ($mutt_attachment) =
1077                 Locale::gettext::gettext("[-- Attachment #%d") =~
1078                 m/^([^%]*)/;
1079             ($mutt_contenttype) =
1080                 Locale::gettext::gettext("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n") =~
1081                 m/^([^%]*)/;
1082             ($mutt_pgpsigned) =
1083                 Locale::gettext::gettext("[-- End of signed data --]\n")  =~
1084                 m/^(.*)\n/m;
1085             ($mutt_beginsigned) =
1086                 Locale::gettext::gettext("[-- The following data is signed --]\n\n") =~
1087                 m/^(.*)\n/m;
1088             ($mutt_pgpclearsigned) =
1089                 Locale::gettext::gettext("[-- END PGP SIGNED MESSAGE --]\n") =~
1090                 m/^(.*)\n/m;
1091             ($mutt_pgpclearsigstart) =
1092                 Locale::gettext::gettext("[-- BEGIN PGP SIGNED MESSAGE --]\n") =~
1093                 m/^(.*)\n/m;
1094             ($mutt_pgpencrypted) =
1095                 Locale::gettext::gettext("[-- End of PGP/MIME encrypted data --]\n") =~
1096                 m/^(.*)\n/m;
1097             ($mutt_pgpoutstart) =
1098                 Locale::gettext::gettext("[-- PGP output follows (current time: %c) --]\n") =~
1099                 m/^([^%]*)/;
1100             ($mutt_pgpoutend) =
1101                 Locale::gettext::gettext("[-- End of PGP output --]\n") =~
1102                 m/^(.*)\n/m;
1103
1104             Locale::gettext::textdomain('gnupg');
1105             ($gpg_WARNING) =
1106                 Locale::gettext::gettext("WARNING: using insecure random number generator!!\n") =~
1107                 m/^([^:]*: )/;
1108             ($gpg_Warning) =
1109                 Locale::gettext::gettext("WARNING: message was encrypted with a weak key in the symmetric cipher.\n") =~
1110                 m/^([^:]*: )/;
1111             ($gpg_Cantcheck) =
1112                 Locale::gettext::gettext("Can't check signature: %s\n") =~
1113                 m/^([^%]*)/;
1114             ($gpg_aka) =
1115                 Locale::gettext::gettext((' 'x16).'aka "%s"') =~
1116                 m/^([^"«%]*)["«%]/;
1117             ($gpg_bad) =
1118                 Locale::gettext::gettext('BAD signature from "%s"') =~
1119                 m/^([^"«%]*)["«%]/;
1120             ($gpg_expired) =
1121                 Locale::gettext::gettext("Note: This key has expired!\n") =~
1122                 m/^(.*)\n/m;
1123             ($gpg_good) =
1124                 Locale::gettext::gettext('Good signature from "%s"') =~
1125                 m/^(([^"«%]*))["«%]/;
1126             ($gpg_bug) =
1127                 Locale::gettext::gettext("... this is a bug (%s:%d:%s)\n") =~
1128                 m/^([^%]*)/;
1129         }
1130     }
1131
1132 }
1133 elsif ($ms_smart || $pgpshort || $pgpmove || $pgpmovevrf) { help(); }
1134
1135
1136 # Read message:
1137 if (!open(IN, $ifile))
1138     { print STDERR "Could not open $ifile: $!\n"; exit(EX_IOERR); }
1139 my @message = <IN>;
1140 close IN;
1141
1142 # First, check msg length and stop processing if msg is too long:
1143 if ((defined $maxlines) && (@message > $maxlines)) {
1144     if ($ofile ne 'NONE') {
1145         if ($mua eq 'mutt') {
1146             my $x = 0;
1147             if (!$nohdr) {
1148                 do { $x++; } while ($x<$#message && $message[$x]!~/^$/);
1149                 $x++;
1150             }
1151             splice(@message, $x, 0,
1152                 ("[---=| Processing by $0 skipped: message too long |=---]\n\n"));
1153         }
1154
1155         write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1156     }
1157     if ($mua eq 'mutt') { exit(EX_OK); }
1158     exit(EX_DATAERR);
1159 }
1160
1161 # this should be self-explanatory:
1162 process_msg(\@message);
1163
1164 # Finally, print clean lines:
1165 if ($ofile ne 'NONE') {
1166     write_msg(($mda?'|'.SENDMAIL." $mda":">$ofile"), \@message);
1167 }
1168
1169 # vim600:set foldmethod=marker:
1170 # eof