From 1679e869df31fecbf305098f05721ba1e8368418 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Fri, 7 Mar 2014 23:16:20 +1300 Subject: [PATCH] Allow specifying the passphrase and key, handle encrypt or sign. --- merge.pl | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/merge.pl b/merge.pl index 13b8839..dc11004 100755 --- a/merge.pl +++ b/merge.pl @@ -8,8 +8,10 @@ use Template; use Text::Wrap; use FileHandle; use Getopt::Long; +use Scriptalicious; use Text::CSV_XS; +use List::MoreUtils; use MIME::Entity; use MIME::Types; use Mail::GnuPG; @@ -65,8 +67,13 @@ for ('subject', 'body', 'from') { # If we've been asked to use encryption, set things up my $mg; -if ($tmplData->{'encrypt'}) { - $mg = new Mail::GnuPG; +my %mg_opts; +if ($tmplData->{'sign'} && $send) { + $mg_opts{passphrase} = prompt_passwd('GPG Pasphrase: '); + $mg_opts{key} //= $tmplData->{sign_key}; +} +if ($tmplData->{'encrypt'} || $tmplData->{'sign'}) { + $mg = new Mail::GnuPG(%mg_opts); } my $IN = new FileHandle; @@ -109,7 +116,7 @@ while (my $cols = $csv->getline_hr($IN) ) { my $file_to_attach; if ($tmplData->{'attachment_col'} || $tmplData->{'attachment'}) { - my $source = defined $tmplData->{'attachment_col'} + my $source = defined $tmplData->{'attachment_col'} && $cols->{$tmplData->{'attachment_col'}} ? $cols->{$tmplData->{'attachment_col'}} : $tmplData->{'attachment'}; @@ -180,22 +187,28 @@ while (my $cols = $csv->getline_hr($IN) ) { if defined $tmplData->{$extra}; } - my $ret = $mg->mime_encrypt($mail, @emails) - if defined $mg; - - if (! defined $mg || $ret == 0) { - $ret = $mg->mime_sign($mail, $tmplData->{'from'}) - if defined $mg; + # Possibly GPG Encrypt the message. + if ($tmplData->{'encrypt'}) { + my $ret = $mg->mime_encrypt($mail, @emails); - if (! defined $mg || $ret == 0) { - $mail->send('sendmail'); - } else { + if ($ret != 0) { + warn "Sorry, gpg encrypt error: " . join(", ", @{ $mg->{last_message} }) . "\n"; + push @gpg_failed_addresses, @emails; + next; + } + } + + # Possibly GPG Sign the message. + if ($tmplData->{'sign'}) { + my $ret = $mg->mime_sign($mail, $tmplData->{'from'}); + + if ($ret != 0) { die "Sorry, gpg sign error: " . join(", ", @{ $mg->{last_message} }) . "\n"; } - } else { - warn "Sorry, gpg encrypt error: " . join(", ", @{ $mg->{last_message} }) . "\n"; - push @gpg_failed_addresses, @emails; } + + $mail->send('sendmail'); + } else { # Say who we want to send to. print "Want to send to: $email_address" . @@ -205,3 +218,9 @@ while (my $cols = $csv->getline_hr($IN) ) { } close $IN; + +if (scalar(@gpg_failed_addresses) > 0) { + print "Failed to send GPG encrypt messages to the following addresses:\n"; + print " ", join("\n ", uniq(@gpg_failed_addresses)) . "\n\n"; +} + -- 2.30.2