Skip to content

Instantly share code, notes, and snippets.

@nemunaire
Created March 13, 2014 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemunaire/9526469 to your computer and use it in GitHub Desktop.
Save nemunaire/9526469 to your computer and use it in GitHub Desktop.
This script changes the Content-type header of each part of a multipart message (only for PGP encrypted or signed parts) ; to allow Mutt to decrypt or verify parts automatically.
#!/usr/bin/env perl
use v5.14;
use strict;
use warnings;
use utf8;
use open IO => ':utf8';
use open ':std';
use Encode;
use MIME::Base64;
use Email::MIME 1.910;
sub walk {
my ($part) = @_;
return if ($part->subparts);
if (grep { /-----BEGIN PGP MESSAGE-----/ } $part->body) {
my $first_type = "text";
if ($part->content_type =~ /([^\/]+)\/[^;]+(;.*)?\r?\n?/) {
$first_type = $1;
}
$part->content_type_set( "application/pgp; format=$first_type; x-action=encrypt" );
}
if (grep { /-----BEGIN PGP SIGNED MESSAGE-----/ } $part->body) {
my $first_type = "text";
if ($part->content_type =~ /([^\/]+)\/[^;]+(;.*)?\r?\n?/) {
$first_type = $1;
}
$part->content_type_set( "application/pgp; format=$first_type; x-action=sign" );
}
return $part;
}
my $email;
# Get the mail
{
local $/;
$email = <STDIN>;
}
die "$0: error: empty STDIN (email expected)\n" unless $email;
$email = encode_utf8($email);
$email = Email::MIME->new($email);
my @parts = $email->subparts;
my @nparts = map { walk($_) } @parts;
$email->parts_set(\@nparts);
print $email->as_string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment