+my $parser = new MIME::Parser;
+$parser->output_to_core(1); # No temporary files
+my $entity = $parser->parse_data(\@MAIL);
+
+#
+# Make sure locale is set to C so we get messages in English as we expect.
+#
+$ENV{'LC_ALL'} = "C";
+
+if ($entity->parts) {
+ # MIME
+
+ my ($got_sig, $got_text) = (0, 0);
+ my ($sig_fh, $sig_name) = tempfile();
+ my ($text_fh, $text_name) = tempfile();
+
+ foreach my $subent ($entity->parts) {
+ if ($subent->effective_type eq "text/plain") {
+ @COMMANDS = split /\n/,$subent->bodyhandle->as_string;
+
+ my $str = $subent->as_string;
+ $str =~ s/=\n$//;
+ $str =~ s/\n/\r\n/g;
+ print $text_fh $str;
+ close($text_fh);
+ $got_text++;
+ } elsif ($subent->effective_type eq
+ "application/pgp-signature") {
+ print $sig_fh $subent->as_string;
+ close($sig_fh);
+ $got_sig++;
+ } elsif ($subent->effective_type eq "multipart/mixed") {
+ my $str = $subent->as_string;
+ print $text_fh $str;
+ close($text_fh);
+ $got_text++;
+
+ foreach my $mixent ($subent->parts) {
+ if ($mixent->effective_type eq "text/plain") {
+ push @COMMANDS, (split /\n/,
+ $mixent->bodyhandle->as_string);
+ }
+ if ($mixent->effective_type eq
+ "application/pgp-keys") {
+ push @COMMANDS, (split /\n/,
+ $mixent->bodyhandle->as_string);
+ }
+ }
+ }
+ }
+
+ if ($got_sig && $got_text) {
+ my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR,
+ "gpg --batch --verify ".
+ $sig_name." ".$text_name);
+
+ close GPGIN;
+
+ @GPGERROR = <GPGERR>;
+ my @GPGOUTPUT = <GPGOUT>;
+ close GPGERR;
+ close GPGOUT;
+ waitpid $pid, 0;
+
+ unlink($text_name);
+ unlink($sig_name);
+ }
+} else {
+ # Clear text.
+
+ my $pid = open3(\*GPGIN, \*GPGOUT, \*GPGERR, "gpg --batch --verify");