gpgit/gpgit.pl

86 lines
2.9 KiB
Perl
Raw Normal View History

2011-01-13 13:23:38 -05:00
#!/usr/bin/perl
##############################################################################
# #
2011-01-24 09:08:28 -05:00
# Copyright 2011, Mike Cardwell - https://grepular.com/ #
2011-01-13 13:23:38 -05:00
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
# #
##############################################################################
use strict;
use warnings;
use Mail::GnuPG;
use MIME::Parser;
## Parse args
2011-02-24 06:26:54 -05:00
my @recipients = @ARGV;
die "Bad arguments. Missing email address\n" unless int(@recipients);
die "Bad arguments. Invalid email address\n" if grep( !/^.+\@.+$/, @recipients );
2011-01-13 13:23:38 -05:00
## Object for GPG encryption
my $gpg = new Mail::GnuPG();
2011-02-24 06:26:54 -05:00
## Make sure we have the appropriate public key for all recipients
foreach( @recipients ){
unless( $gpg->has_public_key( $_ ) ){
while(<STDIN>){
print;
}
exit 0;
2011-01-13 13:23:38 -05:00
}
}
## Read the plain text email
my $plain;
{
local $/ = undef;
$plain = <STDIN>;
}
## Parse the email
my $mime;
{
my $parser = new MIME::Parser();
$parser->decode_bodies(1);
$parser->output_to_core(1);
$mime = $parser->parse_data( $plain );
}
## Test if it is already encrypted
if( $gpg->is_encrypted( $mime ) ){
print $plain; exit 0;
}
## Encrypt
{
$mime->make_singlepart;
my $code = $mime->mime_type =~ /^text\/plain/
2011-02-24 06:26:54 -05:00
? $gpg->ascii_encrypt( $mime, @recipients )
: $gpg->mime_encrypt( $mime, @recipients );
2011-01-13 13:23:38 -05:00
if( $code ){
print $plain;
exit 0;
}
}
## Remove some headers which might have been broken by the process of encryption
$mime->head()->delete($_) foreach qw( DKIM-Signature DomainKey-Signature );
## Print out the encrypted version
print $mime->stringify;