CC: | David Bremner <bremner [...] unb.ca> |
Subject: | [PATCH] Add support for gpg agent. |
Date: | Thu, 12 Aug 2010 13:53:59 -0300 |
To: | bug-Mail-GnuPG [...] rt.cpan.org |
From: | david [...] tethera.net |
From: David Bremner <bremner@unb.ca>
This is is controlled by a flag use_agent to the new() method.
This has been mainly tested with the decrypt operation.
---
It has been suggested that GPG::Interface ought to use gpg-agent by
default anyway if it is enabled in ~/.gnupg/gpg.conf, but it doesn't
for me, even though gpg on the command line does. See
http://www.cs.unb.ca/~bremner/blog/posts/ffac/
for context.
GnuPG.pm | 18 ++++++++++++------
1 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/GnuPG.pm b/GnuPG.pm
index 629880c..0192453 100644
--- a/GnuPG.pm
+++ b/GnuPG.pm
@@ -44,7 +44,7 @@ use Errno qw(EPIPE);
key => gpg key id
keydir => gpg configuration/key directory
passphrase => primary key password
-
+ use_agent => use gpg-agent if non-zero
# FIXME: we need more things here, maybe primary key id.
@@ -58,6 +58,7 @@ sub new {
keydir => undef,
passphrase => "",
gpg_path => "gpg",
+ use_agent => 0,
@_
};
$self->{last_message} = [];
@@ -114,6 +115,11 @@ sub _set_options {
=cut
+sub _agent_args{
+ my $self=shift;
+ return $self->{use_agent} ? ('command_args' => ['--use-agent']) : ();
+}
+
sub decrypt {
my ($self, $message) = @_;
my $ciphertext = "";
@@ -161,7 +167,7 @@ sub decrypt {
);
# this sets up the communication
- my $pid = $gnupg->decrypt( handles => $handles );
+ my $pid = $gnupg->decrypt( handles => $handles , $self->_agent_args );
die "NO PASSPHRASE" unless defined $passphrase_fh;
my $read = _communicate([$output, $error, $status_fh],
@@ -542,7 +548,7 @@ sub mime_sign {
passphrase => $passphrase_fh,
status => $status_fh,
);
- my $pid = $gnupg->detach_sign( handles => $handles );
+ my $pid = $gnupg->detach_sign( handles => $handles, $self->_agent_args );
die "NO PASSPHRASE" unless defined $passphrase_fh;
# this passes in the plaintext
@@ -641,7 +647,7 @@ sub clear_sign {
stderr => $error,
);
- my $pid = $gnupg->clearsign ( handles => $handles );
+ my $pid = $gnupg->clearsign ( handles => $handles, $self->_agent_args );
my $plaintext = $body->as_string;
@@ -744,7 +750,7 @@ sub _ascii_encrypt {
my $pid = do {
if ( $sign ) {
- $gnupg->sign_and_encrypt ( handles => $handles );
+ $gnupg->sign_and_encrypt ( handles => $handles, $self->_agent_args );
} else {
$gnupg->encrypt ( handles => $handles );
}
@@ -844,7 +850,7 @@ sub _mime_encrypt {
my $pid = do {
if ($sign) {
- $gnupg->sign_and_encrypt( handles => $handles );
+ $gnupg->sign_and_encrypt( handles => $handles, $self->_agent_args );
} else {
$gnupg->encrypt( handles => $handles );
}
--
1.7.1